In a vertical UIStackView I have stacked four UIButtons. The UIStackView is centered (vertically and horizontally), the left side corresponds to the left edge of the device and the right side to the right. Inside the stackview, the buttons take all the horizontal space the stackview takes, so the stackview and its content share their width.
Now, I want to reduce the width of my buttons, so that they are 80% the width of the container (on any device except for iPad) and I want to center them too.
How can I do? I need a solution either in code (Swift) and using any possible property I can't find in XCode
Instead of pinning the stack view to both sides of its superview, give it a width constraint that causes it to be 80% of the width of its superview.
The best working solution is
button.widthAnchor.constraint(equalToConstant:self.stackview.frame-30).isActive = true
This works perfectly fine.
Related
I'm having trouble with the size of a UIImageView within the same Size Class (Trait Collection now).
Here's my concrete problem, I have a UIImageView which goes smaller as the device goes smaller, however it's the same Size Class, so I can't add specific Size Class constraints to prevent this.
iPhone 7 plus :
iPhone 4 :
All you can do is remove leading, trailing and bottom constraints and follow the below steps...
1) Fix height and width of UIImageView.
2) And set this UIImageView to Horizontal center of superview.
Now it'll work in all devices without downsize of UIImageview.
Check this,
For iPhone 7 plus you need 3x images and iPhone 4 you need 2x images,imageView has intrinsic content size. So only concern is to position in a right place than giving height or width constraints/by pinning it on four sides. So just center it horizontally to its superview and give vertical spacing to the below view (label or button as I can see there).
I'm struggling a bit with the auto layout constraints in a super simple test view.
For test I added a label, want it x number of pixels from left side, and 5 pixels from top, right, bottom.
When I do this and do an update frame, the view collapse to the min height, and when I insert the view into a tab, the main window collapses and I cannot resize the height.
I can understand why the view collapses to min height, but why does the main window collapse, and why can't I adjust the height.
Which is the missing / wrong constraints.
Thanks
Edit: Noticed I by mistake used a TextField instead of a Label which could have explained the fixed height, but even after changing to a label I see the same issue. If I instead set the height to fixed, so the constraints are left, top, right, height, then I can resize the window as expected, but obviously the label doesn't resize its height.
The label's content-hugging priorities are presumably greater than NSLayoutPriorityWindowSizeStayPut (500). That tells the auto layout system to prefer to resize the window rather than stretch the label beyond the size required by its contents.
Reduce that priority.
So I just returned to GUI programming after a long time on Mac OS X. I heard about this great feature called Auto Layout. For my project I want a very simple layout: a textfield dominating the window, with a couple buttons at the bottom. When I resize the window, I want the textfield to resize with it.
I thought that was simple task: constraints on the 4 edges and Height and Width >= what I have in Interface Builder:
If I set it like this, I can't resize the window vertically, only horizontally. If I drop either the Height or Width constraint, it will shrink to a tiny size in a corner.
How should I set my constraints so that the textfield resize with the window?
To resize text field with the window size you need to add constraints to all the four edges of the text field! As per your screen shot you haven't added any constraints to the right and bottom edge, instead you have added dimensions (or say height and width)!
And to avoid making text field terribly small you can add min-width and height to it.
I have a view in which I placed a scrollView that fits the entire view. Inside the scrollView I have a textView, below a imageView, below a textView and so on. The textViews are filled with content from the ViewController with localized text, so its height will change depending on language. I gave the textViews the constraints for top, bottom, left and right with 10 each. The ImageViews i gave the constraints top and bottom with 10 each, width 200 and centered in container horizontal. The height of the images is different for each image and no constraint for the height is given.
The first result was, the scrollView got a width of the longest text of the localized text. I changed for the
scrollView
Content Hugging Priority Horizontal to 995 and Vertical to 250.
textViews
Content Hugging Priority Horizontal of 400 and 200 Vertical,
Content Compression Resistance Priority Horizontal is 200 and Vertical 750.
Perfect result in portrait mode. When changing to landscape the width of the scrollView stays at 320 and is aligned to the left.
How can I fix the problem to let the scrollView take 100% width of the screen without giving the textViews the chance to force to enlarge themselves to 100% width?
Found solution:
The scrollView has constraints: top/bottom 8 and left/right 0 each to superview.
The textViews have constraints to top, left, bottom and right.
The images have constraints top, bottom and width, centered to X in container.
This results that the textViews take one line and grow to giant width.
Then I centered the textViews to X in Container and selected all textViews and images and centered them too.
Now the scrollView takes 100% width of screen, height as needed no matter if portrait or landscape.
So for all who have maybe the same issue: scrollView looks a bit smaller than the view, its constraints when adding are 0 to left and right and a top/bottom >0. When drag and drop scrollView in the view it takes 100% of the view size. That was the problem, the constraints were -16. Setting the scrollView to x = 16 and width = 288 results in fitting properly to the view! Next giving the textViews constraints top/left/bottom/right and center it to container (even if offset to real center, right to the item is 0 predefined - change it to use current canvas value).
I'm trying to use Auto Layout to minimize the gap between a UIImageView (in green) and a UICollectionView (in red). In the simulator (which I assume is showing an iPhone 4-size screen) the two views line up nicely, with only a small gap between:
However on my actual device, an iPhone 5 with larger screen, there's a big gap:
How can I specify auto layout constraints so that the UIImageView (green box) takes up as much space as available, regardless of screen size, while keeping the UICollectionView pinned to the bottom toolbar?
I've tried experimenting with adjusting the constraints, adding pins, etc in XCode without much success. I feel like I'm just flailing around not really understanding how they work, and I have yet to find a good tutorial that explains how do something like "expand to take up as much vertical space as possible".
So you need to specify that the gap between the two views is fixed, and that the height of the bottom view is also fixed. In visual format language that would be
"V:|[topView]-[bottomView(==150)]|"
V: means this is a vertical constraint
|[topView] means pin the top of topView as close to the top edge of the superview as possible.
- means that the space between the two views is fixed
[bottomView(==150)] means that bottomView must be 150 pixels
]| means that the bottom edge of bottomView is pinned to the bottom edge of the superview
You could also add these constraints in Interface Builder by setting a fixed height on the bottom view, and a fixed gap between the top and bottom views