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
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 going to show you an image that contains my first UIViewController presented in Interface Builder (using the 600x600 Any/Any View).
MainViewController
For now, just look at the UIButton with the C label and the Play button. I added Align center x to: horizontal constraints to both. When I preview what I have (forgetting about the other views and buttons you see), the 2 buttons are centered properly. However, when I switch different device sizes, they do not change size...which makes sense.
What I want is for a way to make those 2 buttons stay proportionally circular and centered on the superview, but adapt their size and Y-position depending on the device size. What constraints would I need to attach in order for that to work? I don't want the buttons to be stuck in the spots you see in the image, I want them to adapt to the device size.
To talk about this further. The game 'Color Switch' does not appear to use constraints in terms of having any view/image/button/label constrained to a certain y or x position. Going from a 5c to a 6s, it's like looking at a blown up version of the game. The buttons and title label at the top are not constrained to the same y position on each device.
You could create constraints to center the buttons to the top-level view and then make their width proportional to the view width. Then on a larger screen, the buttons will be larger and on a smaller screen, they will be smaller.
I am having trouble getting a grasp on how auto-layout constraints work. In theory, one should be able to add a label to the storyboard, then assign a horizontal centre constraint and it should be centred on all devices, correct? However, when I do that, it does NOT stay centred UNLESS I also add a vertical spacing constraint. However, if I test the app it IS centred (without the vertical spacing constraint). This is causing me great confusion.
Secondly, if I add a text input, expand it to full width, then add pin the left and right side, it does NOT maintain the pins in other views. And instead there is this dotted box inside a solid orange line box which is centred, but the element itself is not.
I am at a loss please help. I have attached a video of me doing it in Xcode. Both issues are in there First the centring issue, next to the pinning issue. Thank you!
VIDEO OF THE ISSUES HERE:
https://vid.me/XxFo
-- Red lines mean Constraints are ambiguous. Yellow means warning, Its show you the runtime demonstration of view.
View need four constraint to satisfy its full position.
x, y, width and height
When you give only center horizontally constraint, this is not enough you have to specify the y position as well. Label get its width and height from its text length and text font size.
Though constraints are ambiguous and in preview you are not getting correct result but once you give center constraint, it will be center at runtime.
Run the app and it should be in center horizontally.
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.
I have an NSSplitView with content in both NSViews. The left NSView has 2 constraints – Equal Widths and Equal Heights. The right NSView has something simple, say an NSTextField, which is centered via constraints Center X Alignment and Center Y Alighment. This is what I hoped it would look like as I resize the window and/or the NSSplitView divider:
This is what's happening:
I've tried a great deal of configuration changes, I've tried using an NSSplitViewController vs just dropping an NSSplitView into an NSViewController to adjust more parameters programmatically, but I'm not having any luck. Whenever resizing the window, the left view always takes over the excess space. The same happens with the divider (it can be resized, but letting go of the mouse button causes it to snap right back). It seems there's something fundamental that I'm missing here.
The text field's content hugging priority is probably higher than the split view item's holding priority. Fix that and the view should probably work the way you expect.
Also, if, when you resize the view, the left view is resizing with the window while the right view stays the same size, then that suggests that the left view's holding priority may be higher than the right's. You should make the side that you want to stay the same size have the higher holding priority.
That said, I'm not sure what you mean about the constraints you've set on the subviews. "The left NSView has 2 constraints – Equal Widths and Equal Heights." What do you mean here? Its width is equal to what? Its height is equal to what? Do you mean it has an aspect ratio constraint? Frankly, I can't think of what constraints of those kinds would make sense for a view within a split view.