Align to the edge of the screen on iPhone X - ios

How can I align a view to the top edge of the screen beyond the safe area?

Go ahead a select Top Space to Safe Area to create the top alignment constraint. Then select that constraint in the Storyboard or the Document Outline and examine it in the Attributes Inspector.
Click on the First Item which is Safe Area.Top and in the pop-up select Superview to align it to the Superview instead of to the Safe Area. Set the Constant to 0 and you're done. Your view is now aligned to the top of the screen.
Here it is running in the iPhone X simulator with the red view aligned to the top of the screen:

In the bottom right of that view there are three or four buttons that help you set up constraints. One of them shows you a popup where it lets you create constraints for leading, trailing, top and bottom. (I think it's the second from the right. Xcode is updating right now and I can't open it to verify)
By default it will create the constraints to the safe area but there's a down arrow you can click and there you can choose the view as the constraint target. If you also set the constant to 0 then your view will be aligned at the top to the view.

Related

Button on bottom of screen: iPhone X vs regular screen

I'm trying to optimize an app for the iPhone X screen. Currently it shows black or white bars on most screens. As example I will use the PaymentMethodsView.
The PaymentMethodsView is a custom UIView containing 1 or 2 buttons in a horizontal StackView, the superView has a gray background. This StackView is currently constrained with 16px to the superview (all 4 sides). Currently it looks like this on the iPhone X (and on regular iPhones the view is just on the bottom of the screen:
What I want to achieve here is that the gray area extends to the bottom of the screen, while the yellow sits where now the bottom of the entire view is. (So 16px lower)
I can get this to work by enabling Use Safe Area Layout Guide for this ViewController, constraining the PaymentMethodsView in the viewController to 0 to safe area on the sides, and 0 to superview on bottom. Then in the PaymentMethodsView itself, I change all the StackView's constraints to safe area instead of superview, and change the bottom constraint to 0 instead of 16.
This works fine, except on regular iPhones the bottom 16px (under the StackView) disappears and it puts the yellow button to the bottom of the screen.
How would I go about fixing this?
On iPhone X: The gray area to the bottom of the screen, the button 0px to the bottom safe area
On regular iPhones: The gray area to the bottom of the screen, 16px between the yellow button and the bottom of the screen.
Thanks!
You can do this by adding TWO bottom constraints to your stack view...
First, constrain the grayView Bottom at Zero to the bottom of the view (its superview), not to the safe-area. Then
add one constraint from the stackView Bottom to the bottom of the grayView (its superview) at >= 16
and a second constraint from the stackView Bottom to the bottom of the safe-area at 0 with Priority: 999
This says: keep the bottom of the stackView at least 16-pts from the bottom of its superview (the gray view)
and
The 999 says put it at Zero-pts from the bottom of the safe-area if possible
The result on an iPhone 7:
and on an iPhone XS
Below code worked for me give it a try,
Click on you view controller
Select Identify and Type from right panel
Under Interface Builder Document enable Use Safe Area Layout
Now Wherever you want to place any view to bottom just give constraints corresponding to view not safe area

Bottom Margin in iPhone X using storyboard

Ok I have this case,
There is this screen I designed for iphone 8
As you can see here there is 8px green view that I pinned to the bottom of the superview by making a constrain of 0px between this view and top of bottom layout guide. and it works fine on all phone EXCEPT for the new iPhone X where this happens:
and thats not my intended behaviour. I want to to be as the other iPhones, pinned to the bottom with 0 margin.
Is there is a solution for that using the storyboard? Please post solutions using storyboard only. I know that I can do it programmatically. this should be easy on storyboard I guess.
Thank you
1- Select your view in storyboard

2- Double click on bottom space constraint and select Superview for first item
3- Set constant value to 0
You can achieve this
By Setting AutoResizing like this in storyboard
With AutoLayouts
In storyboard select "View as: iPhone X" and pin the desired view to superview, not Bottom Layout Guide or Safe Area (in my example it's a full screen image view, your view would be pinned only to bottom):
Alternatively, if you are not in iPhone X view, you can pin bottom edge to superview by dragging with ctrl key and then when selecting from the list, press option key - more details in this answer: Autolayout: Add constraint to superview and not Top Layout Guide?

Xcode pin bottom constraint doesnt work

Why the pin constraint doesnt work? I have no constraints, and I want to pin one view to bottom of main view, so I select the child view, then I click the icon from "w:Any h:Any row" in Interface Builder bottom right (Pin), then i click the bottom "I" to create bottom constraint, but if I resize the controller view (in design mode) the view is not moved but something like this appears: (The red line with number - instead of moving the blue view to bottom).
I have pinned it to "bottom layout guide". I also tried to pin it to "view" from dropdown while pinning, but that doesnt work too.
If I look on the "Issue navigator" there is only warning saying "Position and size are ambigious for View"
Without any other other constraints there is no way for Xcode to know what you want to do with the view, all it knows is that you want the bottom of your subview to stay 99pts away from the bottom of the superview, but doesn't know what to do with the top, left, or right edges.
Try adding constraints for the remaining 3 edges, it can be as simple as setting the width and height (the options under the bottom edge in your screenshot), or something more complex depending on what you want to achieve.
You need to define the position of view properly. So that constraints can define position and size of the object.
Like in your case you have only provided constraint from the bottom, which will not be sufficient to define position. So you need to define either Height and Width constraints or top, left and right constraints.
And as long as long red line is being shown keep adding constraints.

How to equally space items in Xcode storyboard?

I want to have 15 buttons equally spaced between them and correctly leading and trailing the superview vertically. I set constraints so that the left set of buttons aligns to the left margin and the right set aligns to the right margin. The first button leads from the top margin in each set, and the remaining buttons each have a constraint for vertical space set to the upper neighbor. I thought this would be enough to make it work but it wasn't. As you can see, for an iPhone 4, the number of buttons displayed on the screen is only 12 so I would definitely need to somehow dynamically change the button size depending on the screen size. I am attaching a screenshot from the storyboard that shows the Any x Any editor view and a preview for the 3.5-inch screen.
This was really easy to accomplish using stack views. Simply select all the buttons and click the leftmost icon (bottom right side of left pane) which is named Stack, and see the magic happening!
Try to set the constraints like this:
First select the all elements on left side and give leading constraint:
Now select first element and give top constraint:
Now select the second element and select it , and drag it to first element and give vertical spacing: , repeat giving vertical spacing for all the elements, So that each element will be vertical space constrained with its top element!!.
Do the same for elements on right side.
Give the trailing constraints for all elements on left,
and the select top element and give top constraint
and then select one element below top element and give vertical spacing between those two, repeat it for all the elements!!! So that each element will be vertical space constrained with its top element!!
Note: I only gave left constraints, you can try for right side too!!!!!
Add height constrain to the top left button to be 1/15 of the superview height and for all buttons let their height equal to the top left one.
Note it will work only if the vertical spacing is zero between buttons.
If you have a vertical space value for example x, then set it as 1/15 - 14 * x
Note #2 in iOS 9 only, seems the stack view will help perfectly in that

How to put buttons at the bottom of the label?

I follow Developing iOS 8 Apps with Swift course. The calculator is being developed there. As the first step, I add label (display) with the following constraints -
right - trailing space to Superview
left - leading space to Superview
top - top space to Top Layout Guide.Bottom with value 8
Then I should add the button (digit 7 in my case) and put it in the left top corner, but below the label. But it doesn't work - when I take and move the button (or all buttons as shown on the screenshot below), then the left blue line indicating left constraint appears, but there is no line indicating bottom constraint of the label.
What is wrong there? How to place buttons exactly below the label?
First of all in your case you should embed all the buttons in a single view and then add constrains in that view relative to superview (be aware that now buttons should have constrains relative to newly created view). To do that select all the buttons go to Editor->Embed In->View. The created view cant have top-left and below display constrains in same time because top and below are conflicted constrains. So next add the left constrain for the view and vertical space between display and view with following rule : Display.Bottom equal View.Top with a constant of your value. Finally your 7 button should be placed top left of its superivew

Resources