I have a UIViewController with a UITableView. The hosting UIViewController is presented by a UINavigationController. I am using autolayout and have used "Leading Space to Superview (-16.00)" and similar "Trailing Space to Superview Constraint" to pin the tableview to the left and right side of the parent view. It works great in Portrait mode. But when I switch to Landscape mode, the tableview is short by a few pixels on each side. Screen captures are shown below. Notice the small red border on the Landscape version. (I have set the background of the view, under the UITableview, to red.) I can even see this border in the storyboard, but I can't figure out how to get rid of it.
My app is built for iOS 10 if it matters.
Leading Space to Superview
I'm going to suggest that you've pinned the leading/trailing edges to the superview's margins. You want to pin them to the superview itself (and change the constant value to 0).
Related
I have a UIScrollView that contains everything else in my ViewController. I use it because I have logic to scroll up the view when the keyboard shows.
I'm working to make my view compatible with variable screen sizes to accommodate newer iPhones. Currently there are no constraints on anything in the ViewController. If I open the app on a device, it appears in the top left corner.
The first thing I tried was pinning the left/right sides of the UIScrollView to the edges of the container, with left/right distances of 0. When I fired it up on a device, the view had disappeared entirely.
I then tried adding a Horizontal center in container constraint the scrollview (removing the other constraints). Again, just a blank screen - everything had disappeared.
What am I doing wrong? How do I make my scrollview fill the screen, or at least be centered on wider devices?
Is there any way to debug layout issues like this? At the moment, I have no insight into what has happened to the view.
Adding height/width constraints, even though these constraints were overridden to stretch the view to the viewport, fixed this problem.
I was playing around with iOS 8 auto layout and constraints and ran into this simple issue, and wondering if someone can help me.
All I want to is to create view (uiview) and have a label at the bottom center.
So I created a new project, Main story board is set to wAny hAny.
Dragged a view into it
x: 16, y:20
Width 275, Height:560
Color : Green
Added the following constraints
Pin top space to superview
pin leading space to superview
Pin hight
Pin width
Added a label and centered it to the bottom of the view
on the label, pinned leading space to superview
pinned bottom space to superview
Currently not seeing any constraints error
using iPhone 6 in the simulator
in portrait mode, I can see the full view and the label at the bottom
Now rotated to the left which is now the screen is in landscape mode. but the view is still on the left side of the screen vertically and 1/2 of it is cut.
So my question is how can I fix this issue so the view also rotate or I can the whole view with the label?
Generally you don't want to be pinning to one side and not the other because as the view size changes (such as with rotation) only one side of the view will get pulled, and that's exactly what you're seeing.
So, either you should not pin and instead centre horizontally in the superview (and allow the intrinsic content size of the label to set the size) or you should pin both leading and trailing (so the label will always be resized to full width).
I have a viewController, which is configured for the iPhone5 screen size. In red rectangle is the view.
To make this view show properly in iPhone4 - I used auto layout. But when I specify constrains such as: leading edge, trailing edge, bottom space and top space (in top space I even made constraint's priority to LOW ) - the view still goes partially down the screen as if my constraints don't work.
BUT if instead of top space constraint I specify view's heigh and delete top space constraint - everything works perfectly.
Does anyone can explain it please? Thank you.
Thats because when you set up the top space constraint it will move the view by the constant you provided. iPhone 4 and iPhone 5 screen height is different but the constant remains the same so obviously it will behave differently. One way to troubleshoot your interfaces is to switching between iPhone4 and iPhone 5 on storyboard device on storyboard (first button from left to right on the bottom right corner of interface builder).
Auto-layout is all about experience in my opinion. I struggled alot with it until i learned. If you want your view to be attached to the bottom of the screen you should set the BOTTOM SPACE to 0 and specify the view's height like you did or adjust constraints for it's subviews so that the height is set dinamically according to the views inside.
I'm developing an app targetting iOS6/7, and I've lost two hours staring at a storyboard, trying to understand why autolayout doesn't do what I want it to do.
Basically, I have a scene containing a scroll view and in it, I want to have a UIIMage anchored at the bottom right. Therefore, I set four constraints for the image:
Width equals
Height equals
Bottom space to superview and
Trailing space to superview
XCode does not complain about the positioning, so I run my app with confidence, only to find that it is not shown in any orientation. It's just nowhere to be found!
I know that to find how autolayout implemented the constraints and did its magic, I have to inspect the view.bounds rect. I checked at the viewDidAppear event, to find its value to be as expected though:
Image pos is: 0.000000,0.000000 106.000000x103.000000
The frame is of course at the actual position in the storyboard which I guess is to be expected.
Here is a screen cap of my Storyboard:
Any ideas?
Update:
Some more info:
If I remove all constraints and run the app, the image view is shown at the bottom right of my view in portrait, but when rotating, as expected, it is not shown.
Update 2:
This all should fall in the dreaded UIScrollView and AutoLayout threads. In the end I reverted to using a UIView, inside of which is a UIScrollView containing all the content I wanted to scroll (so that no text fields are hidden by the keyboard in landscape mode). The image I wanted anchored at the bottom was left at the container UIView and it all worked as intended.
If you want to anchor to bottom right, Programmatic autoresizingMask has been far more consistent for me.
It's slightly opposite of IB so if you want to anchor bottom right, you want the left margin flexible and the top margin flexible
yourView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
This means that bottom margin, right margin, height and width will all remain the same, keeping it in the lower right of your view.
In Interface Builder I have my main UIView however inside of this I have another UIView which acts as a header (different colour background and contains centered text whereas the main UIView behind and seen below the header contains other centered text).
The app was originally built for portrait mode. Now that I have allowed landscape mode, when I rotate the device, all the text auto resizes to remain in the center of the screen, but the header view remains the same width and locked to the left of the screen so I have a lot of trailing space to the right of it as it doesnt go all the way to the other side of the screen.
After looking for a solution I still haven't found one which fixes the problem so how do I set this UIView to stretch automatically on orientation change like all the text labels?
This is simple using auto layout. Give that view a constraint to the left, right, and top of its superview, and no fixed width. You can give it a fixed height if that's what you want, or give it a height that's proportional to the height of the screen (this would have to be done in code) if you want it to be shorter in landscape.