Why the layout is different between storyboard and the simulator? - ios

As below image shows, in the storyboard, I put a UIScrollView (say called A) under navigation bar and another UIScrollView (say called B) inside A. B is at the top of A.
However, in the simulator, there is big margin between B and the up bound of A (I didn't use any autolayout or write any code). Can anyone tell me the reason? Thanks.

add the below line in your - (void)viewDidLoad method
self.automaticallyAdjustsScrollViewInsets = NO;
hope it will fix your issue.

Even though you're not using Auto-layout, you have Auto-layout checked on in your Storyboard. Because you do, UIScrollViews and any Subclasses of it (UITableView, UICollectionView, etc.) all automatically adjust for the UINavigationBar height when in a UINavigationController. This is implicit and there's no way to turn it off. The only solution is to "offset the offset" or to move the UIScrollView away from the UINavigationBar.

The big margin size is navigation bar height, try add a top autolayout for your scrollView.

Related

Subclass UITabBar without Storyboard to increase height

To my understanding, the only way to subclass UITabBar (to increase the height) in a UITabBarController is to implement a storyboard and assign the subclass directly via the interface builder.
I was wondering if it would be possible to simply swizzle self.tabBar to our own subclassed tab bar, but that probably wouldn’t work.
Any other ideas? I’m simply trying to increase the height of the tab bar, while not breaking or conflicting with the superview’s constraints.
I think you should go through all the answers given in the following link.
Change UITabBar height
In this example people have given ways to customize height without even subclassing.

UITableView contentOffset for headerView misaligned

My headerView in my tableView should go all the way to the top of the device. On iPhone X the same so the notch goes over it.
Then when I scroll my tableView I use scrollViewDidScroll(scrollView: UIScrollView) to change the height of my headerView to add scrollView.contentOffset.y so it goes all the way.
It works as soon as I scroll but not initially.
So it looks like this:
and this on the X
How to I get rid of the white area so the grey one goes under it when it loads?
I tried a couple places but it seems the tableView still has no contentOffsets... I think I am missing something fundamental here how "it should be done"...
Update: ViewHierarchy, blue is tablewView, but then I get this distance there...
Ok I found it. So it has to do with the change with safe areas introduced for the iPhone X.
There are two possibilities, it really depends on the storyboard setup:
If you are using a storyboard and your base is a UIViewController with a tableView placed inside it:
Then fixing the auto layout is the answer:
When safe area is enabled, click on your topmost view and create the top constraint is relative to the superView with constant 0. By default it wants to protect the safe area so it is set relative to the safe area.
If you are using a UITableViewController template (e.g. default when dragging out a navigation controller - tableview combo (my case))
Here there are no constraints that can be set. Hence it didn't work for me (see above).
To fix it click on the root tableView, select the size inspector and then set "content insets" to "never".
Or programmatically in viewDidLoad:
self.tableView.contentInsetAdjustmentBehavior = .never
Are you making your tableview in storyboard?
in viewDidLoad try anchoring your tableview:
tableview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

UITableView leaving gap below transparent UINavigationBar (within single UIView on UIViewController)

I am working on a Swift iOS8/9 project and using Xcode 7, with storyboard / autolayout, etc. (For completeness: I started at the beginning on the year on older versions of Xcode, etc.)
I have a a UINavigationController hierarchy and some View Controllers contain a single UIView which contain a single child, a UITableView. (I am choosing not to use UITableViewController, so suffice to say ... it is not an option.)
The problem: The tableview starts 64 pixels (status plus nav height) too low:
Now: I've had things working for some of my UIViewControllers containing a UICollectionView within a UIView but I achieved it (after lots of poking / prodding, and a while back) by a top alignment constraint from the top of the UICollectionView to the top of the Top Layout Guide. (Looking through my storyboard I some alignments and some vertical distances, but all to top of Top Layout Guide.)
I am looking for help understanding if I can do what I want, if the approach of constraining the top of the tableview to the top of the layout guide is correct, or if there is a better way.
Unfortunately, I cannot seem to repeat this (perhaps 'cos I've forgotten some key press combination jujitsu, or new Xcode, or collection != table, or something in my view hierarchy. (I've checked all segues are adaptive push, I've even deleted/re-built this VC since I had one model segue, just in case it was confused that this VC isn't in a navigation view controller hierarchy.)
If I control/drag to connect or select the two to align:
this is what I am offered - note no "align to top":
Note: Here are the table settings that (to my understanding) should be adjusting edge inset and scroll offsets correctly, and do appear to ('cos it is the only child of the top view, per documentation.)
Here are the frames after view did layout subviews:
The approach that I ended up using came to me when I spoke to #ProgrammingBonn and mentioned the frames. I saw that the primary UIView was position as I wanted, and so it (finally) dawned on me to align the top of UITableView to the top of the UIView.
(I suspect that much as I was building my own ViewController and not relying upon UITableViewController I had an over inflated view of this particular view within the hierarchy. It was a subview not a primary view, despite occupying all the screen real-estate.)
So, to be clear:
I selected the UITableView, then command/selected the UIView (the main one, and it's parent) and then used the "align" tool to top align those two.
Try unchecking the "Adjust Scroll View Insets" option.
I am not sure whether this will fix your problem but have you tried playing around with viewController.edgesForExtendedLayout?
See more info here:
Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

iOS: Why is my subview appearing with a big spacing inside scrollview

I have a single scrollview in my view controller and several subviews (textfields and labels). These subviews appear grouped in the middle of the screen, although I added constraints.
How can I make them appear with the standard spacing to the navigationbar?
It might be a problem with translucent nav bar.
Turn on the nav bar translucent and see if the views are correctly positioned. If they are, you have to adjust the spacing in IB.
Autolayout doesn't work the same way as everywhere else when inside a scrollview--it's a whole new ballgame (as described in apple's documentation).
If you place a UIView within the UIScrollView, and make sure you have that view the same size as the scrollview's content, you can use autolayout in this new view to place all your textfields/labels however you like.
For more info, see apple's docs on scrollviews & autolayout.
setting
self.automaticallyAdjustsScrollViewInsets = NO;
or, alternatively and corresponding in the storyboard, did fix it for iOS7. Don't know if this works for iOS 6

Can't adjust autoresizing mask for viewController

I'm trying to adjust my app to iPhone 5, but I ran into some difficulties.
The header of the ContainerViewController (always stays on top)
The ContainerViewController that holds the other ViewControllers
SidebarViewController
ContentViewController
So, the container contains the header, sidebar and content views. Inside content view I have UINavigationController. My problem is when I use iPhone 5 it changes all of the application looks, even when I use Autoresizing Mask, it looks bad and not on the correct position, I think that it's maybe because of the container header that I created.
I set it like this:
And this is an example of the view that holds UIScrollView and UIImageView as background: (pay attention to the bottom of the UIScrollView and the UIImageView, it pushes them outside of the view.
iPhone 4:
iPhone 5:
This is the first time i'm using Autoresizing mask, so please be patient with me. Thanks in advance!
Am not going to answer this deep down. You have set the auto resizing wrong.
Here are things that will work. To deal with iPhone5 you just need UIViewAutoresizingFlexibleHeight as autoresizingMask
If you want to know properly about Auto-resizing read through this
For Container
For Header
For Content
For Side Bar

Resources