I'm totally new to Swift and in general, to iOS development. I just want to center a rounded profile picture with a table view above. In iPhone 5 it looks centered, but in iPhone 6 and 7 it look aligned to left, I don't know how to fix with constraints, I'm a little bit confuse.
This is my constraint tree:
How it looks storyboard for iPhone5
How it looks storyboard for iPhone 6/7
This is how it looks in emulator with iPhone 6
Can you guys help me to figure out what I'm doing wrong?
Thanks in advance!
Cheers.
It looks like you've set the width and height of your image view to hard-coded values, which will result in it looking centred for some views, but not all.
Reset your constraints, then set your distance from each of the four sides to zero. That'll centre your image!
Go to Editor -> Resolve Auto Layout Issues -> Reset to Suggested Constraints...
...then click that button down the bottom right and set each of your four sides' distances to zero.
Make sure you set Update Frames to "All Frames in Container" before clicking "Add 4 Constraints".
Try setting horizontal constrain between your My UI Image View and HeaderView. You can set centre hozintal with HeaderView.
Go to button down the bottom right and set centre vertical and centre horizontal to your My UI Image View.
Hope it helps. Happy Coding!!
Related
XCode 9.3: macOS 10.13.4
I have an swift app where the buttons are off-kilter. I need them at the bottom of the screen and centered. The iOS simulator for iPhone 8 shows the buttons correctly. However, the simulator for the iPhone SE and the actual SE (my iPhone) show the buttons anchored to the right edge of the screen. How can I insure that the buttons are displayed correctly on all iPhones?
First sol
this layout can be done correctly if the center of the middle button is equal to the center of the screen
Second sol
create a UIStackView with these constraints
1- left , right , bottom to screen
2- height say 100
with distribution set to fillEqually , spacing set to say 20
drag the buttons inside it and they will look right in any device
Remove the constraints from the 3 buttons: Calc, Clear, and Help
Align clear to be horizontally centered
Align Calc trailing offset from Clear's leading.
Align Helps Leading offset from Clear's trailing.
Align Clear bottom offset from the bottom margin.
Align Clear, Calc, and Help along to center vertically with one another.
You have two choices: 1) use the legacy struts and springs mode in interface builder to distribute your items as you need them or 2) spend the time learning auto layout, and use it instead. There isn't any easy answer because of all the different screen sizes, and you haven't even considered iPad screens here. The best way to pick up auto layout is to view the WWDC videos on the subject. Yes, it will take longer, but your app will respond better to screen changes, and you will also be able to inherently accommodate new screen sizes.
Use a horizontal stack view for buttons, and set spacing between buttons.
Approach 1 - Set trailing space and leading space, pin the view to bottom and set a fixed height for stack view.
Approach 2 -
Set fixed height and width to stack view and center horizontally, and pin the stack view to bottom.
Always try to use stack view, with that you will reduce the number of autolayout issues you run into.
Please add the constraint for stack view and adjust like that:
I am working on an ios app where I want to have two buttons at a fixed distance from each other. Something like below:
I tried dragging one constraint between the two images. But when I run it in simulator it shows overlap as below.
Can someone guide how to put both the pictures at a fixed distance. Also, I am seeing when I turn the simulator as right in hardware option some of my buttons are not getting auto aligned to horizontal screen and doesn't get displayed. How to fix this too?
Thanks
First of all clear all constraints. and follow below steps :
select both button with command button press
Give Width & Height constraints for both. (Pin at bottom-right side)
give Horizontal Centre in container constraints for both. (Align at bottom-right side)
Now, update all constraints (Resolve Auto-Layout Issue bottom-right side)
Hope, it's work..
Design your view according to this image
Drag a new view and then put your two button in it
Constraint for new view
Top
Horizontal center
Fix height and width
Constraint for left button
Top and leading
Fix height and width
Constraint for right button
Top and traling
Fix height and width
It will work defiantly.
I build a view that has contents which exceed the height of the iphone 6 screen vertically.
To test that out, I've set a specific height for my image (600pts) to guarantee that the whole content wouldn't fit on the screen of an iphone 6 and that you should swipe down to see the rest (the label "BYE").
However, when I run the simulator, I cannot swipe down to see the rest of the screen.
I've used Autolayout constraint and set a margin top/bottom between each components and the superview (I don't have any autolayout constraints issues/warning).
Here's the screenshot of the storyboard of my UIViewController:
Here's when I run the simulator, I cannot see the Label "BYE"(which is normal) but I cannot swipe down to see it.
I am using Xcode 6.1 and Swift.
Any suggestion and explanations?
Thanks a lot!
I have redone your screen and here are the steps. The reason for your issue is not setting proper constraints in autolayout. First you need to choose AnyWidth AnyHeight in Xcode which is in bottom of the storyboard.
Here is the screenshots of View in StoryBoard and its constraints
Constraints
Final Result in Simulator
Setting the constraint properly will work for you. Let me know if you have any issues in doing so. Sorry for the image i used its low quality one and its just for test.
EDIT
In Storyboard if you see the bottom of the screen you will see below image. In center of that image there is wAnyhAny.
you need add constraint to thoes components!
Otherwise, some components will be placed out of the screen!
You can learn how to use Autolayout from here
![enter image description here][1]At right bottom of storyboard in xcode you will see triangel Resolve autolayout isuue select in Reset to suggested constraint.
I'm using Interface Builder in Xcode 6 to make an app and am having trouble getting the text fields and button to centre on the screen for different size screens.
I thought it was a matter of selecting horizontal and vertical centering in container but it doesn't seem to be that when I try it in auto layout. Actually I've tinkered around a bit and I still haven't got it.
I just want to be able to see all of my button and text fields for any size iPhone screen and right now simulator is only showing part of these UI elements like this:
I also want to do this in storyboard and not in code as I'm not at the level of doing this in code yet.
Step 1: Make sure your size class covers all the iPhone screen at least in portrait view. So, change the size class to "wCompact hRegular".
Step 2: After setting the size class properly, add the UITextFields and UIButton to your storyboard. To me, it looks something like-
Step 3:
Before, you start adding constraints, you need to remember two things-
a. Your element(UITextField, UIButton, UIView or any component) needs to know its starting position unambiguously, and
b. Your element needs to know its size meaning, its height and width.
In this case, as you want to centre your elements, I am just assuming that it needs to be centred starting from 10 scale from the left edge and should end 10 scales away from the right edge of your iPhone screen. Now, that means, it's width will be different based on the screen size, but its height will be same.
So, I just add the constrains following way for the 1st text box-
Notice, in the size inspector, I set the text box's starting point, x and width in a way that it is 10 pt away from left edge and 10 pt away from the right edge. Don't worry, it's just simple math.
For the 2nd textField, I add the constrain, the same way-
Lastly, for the button, the constrains are following-
Now, you are good to go. Everything is centered.
By using your size class selector in the bottom of the storyboard window, set you sizes as any width and any height and then follow the below auto layout constrains. It will work for you.
First select the view you want to set the auto layout, and then select the pin option from the right bottom corner of your storyboard and then add the constrains as shown in above picture and click button Add 4 constrains
Repeat the process for all views and set the constrains as Fix the top, bottom, left and right constrains of all views except the last button that should be fix from top,left,right and fixed height.
You need to make use of the size class selector in the bottom of the storyboard window.
So for an iPhone 6 or 6 Plus in portrait you would choose a compact width and regular height like this:
And then you would do whatever auto layout stuff for the given device there
I have simply Drag and button and set Image on it as following
Yet I have not used any CONSTRAINTS. As I run on iPhone 3.5 inch, it shows like below, where Button goes slightly down towards Y-Axis.
I have TWO Questions here to ask.
1. Which Constraints should I add to this Button?
2. Why these constraints are added, Simply I want to know Formula for calculating it.
I would be thankful, If someone comprehensively describe my questions.
Thanks
Question 1:
This should be pretty easy in IB, using the Alignment Constraints dialog. Click the button, and in the Alignment Constraints, check Horizontal Center in Container and Vertical Center in Container. In the below collage, you can see how this centers the button on both 3.5 and 4 inch displays.
(Don't forget to click the button "Add 2 constraints" :-) )
Question 2:
Not sure what you are after... Formula?
select button
click resolve autolayout issues (bottom right)
add missing constraints
Question 1 & 2:
It is depends on your requirement and layout design.
adding constraints for alignment or spacing, you can also use this menu to resolve layout issues, and determine constraint resizing behavior.
Align. Create alignment constraints, such as centering a view in its
container, or aligning the left edges of two views.
Pin. Create spacing constraints, such as defining the height of a
view, or specifying its horizontal distance from another view.
Issues. Resolve layout issues by adding or resetting constraints
based on suggestions (see “Resolving Auto Layout Issues”).
Resizing. Specify how resizing affects constraints .
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/WorkingwithConstraints/WorkingwithConstraints.html#//apple_ref/doc/uid/TP40010853-CH8-SW1