Swift UIView disappearing from Stack View - ios

I have a label, an Image View, and a button in a vertical stack view. The alignment is set to fill, and the distribution to equal spacing with an offset of 10. On my storyboard, the label appears on top, the image in center, and the button at bottom.
However, when run on simulator or device, the UIView is stripped from the view entirely, so the label appears at the top of the screen, and the button directly below the label.
My only constraints are set on the stack views to the superview
Edit: Forgot to mention the UIImageView is inside of a UIView

I had a similar problem. My UIView in my UIStackView had only a Width constraint and would disappear at runtime. I found I could make it show up again with either of these options:
Add a Height constraint
Add a Top and Bottom constraint

I was having the same issue with a tableview in a stackview. Added the Height constraint to the view and it worked now everything shows fine

Setting a Y constraint on the UIView border caused it to appear when hitting the play button, but it grew outside of the screen bounds.
I had to set size constraints on the UIView in order for it to display properly.

Related

UIScrollView not scrolling no matter what I try

I have a screen where the main view has a UIScrollView which contains a UIView which in turn contains 3 UIViews placed vertically, I have set the constraints accordingly, trying setting contentSize in both viewDidLoad and viewDidLayoutSubviews, all to no avail. I have created a sample project with the issue I'm facing at https://github.com/modsoussi/ScrollViewTest.
I'm surprised Interface Builder isn't showing a Constraints error or warning...
When using auto-layout, the contents of the scroll view define the contentSize. That means the contents (in your case, a single UIView holding 3 smaller UIViews) must have a constraint to the Top, Leading, Trailing and Bottom of its superview (the scroll view)... and it must have a Height and Width.
In your storyboard, delete the "Center Vertically" constraint, and add a Height constraint (of 1000) to the "content" view. Then delete your scrollView.contentSize = ... line from code. Run the app, and you should have no problem scrolling.

iOS layout: How to keep a button in the middle an image?

I have an image that needs a UITextField in the middle of it at all times. However, I can't get it do what I want.
I pinned the image with top, left and right constraint and gave it a height constraint. I put the UITextField in the middle of the image and tried various methods to keep it in the middle, but they all keep failing. Any tips are appreciated. Here is what the image currently looks like. First one iPhone 4 and second one is iPhone 6
You can just select the image view and the button and create two constraints: a “Horizontal Centers” constraint and a “Vertical Centers” constraint. Neither view has to be a subview of the other, and they don't have to be in the same superview either.
Here's a demo. I've already constrained the image view to fill the top left quadrant of the root view, and I've dragged in a button. In the demo I'll select the image view and the button (hold shift to select multiple views) and create the two centering constraints. Then I'll use the Preview assistant to show that the button stays centered when the image view's frame changes.
What you need to do:
Embed the UIImageView and UITextField inside a UIView.
Set constraints of UIImageView leading, trailing, top, and bottom to UIView with constant of 0.
Set constraints of UITextField center horizontally with container view, and center vertically with container view.
Then it's up to you on where you want to position that UIView. This will definitely always position the UITextField to the center of UIImageView.

UIScrollView does not fill whole screen

I have a UIScrollView which is set up with AutoLayout. I have a constraint on the "Save" button from the bottom of the button to the bottom of the scrollview, set to 0. The scrollview also has a bottom constraint of zero to the superview. Meaning, that the Save button SHOULD be 0 pixels away from the bottom, and even says so on the attributes inspector (see screenshot). How come, when I run the project, or even view from the storyboard, that this button is NOT at the bottom of the screen? Why is the save button refusing to align on the bottom of the scrollview and thus the bottom of the screen?
According to this article: https://www.natashatherobot.com/ios-autolayout-scrollview/ , the best way to deal with scrollview is to make sure only one view (we will call this contentView) is within the scrollview. Place all the subviews within contentView, and the trick is to use equal height and width from the scrollview to that contentView. I was able to solve my issue that way.

Strange behavior of constraints, iOS8

I have two root views on superview.
All views and constraints I've added from code. Top view have H:|-0-[view]-0-|. Same vfl code have bottom view. Next, top view attached top to top, bottom to top of second view, second bottom attached to bottom. The code is V:|-0-[topView]-0-[secondView]-0-|.
Second view have intrinsic height, so height of both views depends on this value. When I change bottom view height and animate layoutIfNeed all works fine for me.
Next, bottom view have some subviews. Bottom is simple view-container and top is button. Both of them attached to left / right, like H:|-0-[view]-0-|. Bottom view has intrinsic height too, so vertically them attached like outside views, without (!) attaching button to top of superview (V:[button]-0-[secondView]-0-|). Second view is ATTACHED bottom to bottom, but when I animating changing height of this container, its subviews go top on iOS 8.
View hierarchy is
Constraints:
H:|-0-[topView]-0-|
H:|-0-[bottomView]-0-|
V:|-0-[topView]-0-[bottomView(110)]-0-|
H:|-0-[buttonBackCamera]-0-|
H:|-0-[grayView]-0-|
V:[buttonBackCamera]-0-[grayView(85)]-0-| //here is constraint, that MUST attach grayView to bottom.
While debugging, I noticed, that constraint is attached, active and it must work, but :( .
Any thoughts about this?
UPDATE
I've checked this on iOS 8.3, all works fine too.
Make sure bottom view contains constraint from Leading to superview, Trailing to Superview and Bottom to Bottom Guide.
And if you are still facing any issue then go for,
-(void)viewWillLayoutSubviews
{
[self.view layoutIfneeded];
}
I don't think I got the problem entirely but may I suggest you to check the size class that is selected and the size class for each of the enabled constraint.

Adding UIButton to custom cell with auto layout

I am trying to add a button through IB in a custom cell at top right corner of the cell. I am using auto layout to position it correctly but in simulator I don't know why the button wont show up.
But if i create it programmatically then it shows up perfectly.
So my question is how can achieve it using IB and where i am doing it wrong.
I have added four constraints on my button leading and trailing space to superview and fix width and height.
Remove leading space constrain and add fix to top space constrain. I mean there will be total 4 constrains Fixed width, height and fixed trailing, top space to super view and it will work. If it is not working means you have not set your tableView constrains. For tableView fix it from top,bottom,left and right space from super view.

Resources