How can I make UITextField always centered on the screen? - ios

I'm making an app in which I add people to a list by using a modal VC, that currently has only three text fields.
Problem:
This is what I see in the interface builder, inside my Main.storyboard file:
I'm running Xcode version 6.2 (6C131e) and the app I'm making will be for iPod touch only, so I disabled auto-size classes. I'm expecting those text fields to be at the center of the screen (same distance from text field lateral border to screen edges). Instead I get this on the simulator:
Question:
How can I set an equal distance for both the sides of text fields? Any resource like tutorials, examples, guides are appreciated, because I am a very beginner in iOS programming. If this can be done with interface builder for me is better then programmatically.

As simple as this:
In the Align menu from the bottom of interface builder if you set this constraint to 0 it will be the center of the view

Select First Textfield
Goto Pin-> Uncheck constraints to Margin-> Select Top,Left,right constraints with default value(or change if want ex. 16 left,right,30 top).
For Second and Third Textfield
Goto Pin->Uncheck constraints to Margin->Select Top,Left,right constraints

If you are not using Autolayout then the default Autoresizing property should be
Change the Autoresizing property to this :
Check this question : Any other method to resize views, other than Auto-layout

I just take horizontally as example. You have two ways to make it in the centre.
1. Make leading space equals trailing space.
Make the textfield center horizontally in container view.
I recommend you can study with raywenderlich tutorial.
http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

Related

How to set margin to zero between views in storyboard Xcode11

I have a question while developing iOS application using Xcode11.4.
This is my first time to developing iOS, sorry for my stupidity.
Storyboard development is too hard to me.
This is what I wanna make.
The way I did is make a stackview and put two textviews in stackview.
And it's not problem so far.
< My storyboard >
There is vertical margin between textviews, so I tried to set them to zero using View inspector.
But it is not working, and Xcode displaying constraint error.
Is there any better way to set margin to zero between vertical TextViews?
Thanks.
Select your stack view > now select attribute inspector which is showing with blue icon in this image
now edit spacing as you want

How to center TextField using Xcode constraints?

I am new to iOS and am trying to align these TextFields in iOS. Below is how I would like it to look on an iPhone 7.
However, on the iPhone 7 Plus, the TextField shifts to the left of the center, despite my constraints.
Why is this? I have a constraint that aligns them to the Center, as seen on the right part of the screenshot.
It is very simple!! Just follow below steps,
Open you .xib or .storyboard file
Select view you have to add constraint
Click on align button at the right - bottom corner
Set horizontally in container constraint 0
Then press add constraints button
You can add any constraints to any view by following these steps
You just have to seth trailing and leading space fixed means both left
and right indicators should be red as shown in the screenshot.
For more details see the screenshots:
It will remain fixed from left and right in all the screens.
in your case is important set the width constraint.
However you can use trailing and leading constraints, in this way you will not need to set a defined width for your textfield and the position is correct for all type of devices.
If you want design like this so you have to take UITableview then take UITextfield for every cell beacuse you use every cell as view so easily manage all textfield and constraint. And also easily scrolling if device is display small.

iOS multiple screen not working?

I just started iOS programming and I want to make a login screen. I want to align center horizontally the everything on all devices like the iPhone 5 and iPhone 6 and iPhone 6 Plus but it's not working. I just put the elements on the screen. Anything else i need to do?
There are a few ways you can do this...auto layout as #ozgur said is your friend.
what I would do is throw them into a Stack View
To do that tap each horizontal group so for example "username" label AND the text field to the right and while they are both selected tap the icon at the bottom right that is on the left of the 4 icons (it has a downward arrow)...now do the same for the password...now do the same for the login button...now select all 3 stackviews and tap stackview again...
NOW....on the left navigation select the topmost stackview and then again on the bottom this time tap the small icon to the right of the stackview button...to make it simple and in the center tap horizontally in container and vertically in container then add the 2 constraints....
you will also need a height and width so now once more with the whole thing selected tap the |o| looking icon to the right...now give it the height and width you like but before you tap add 2 contraints at the bottom of that menu is a drop down menu..tap it and select "update all frames in container"
that should do it
Use AutoLayOut Constraints to fix your issue.
Contraints will set the spacing with the view.You have to apply it on every single element of your view.
if you only want every thing in center no different spacing for different iPhone use stack View. it will provide the best solution for you.
stackView means collection of your objects. it act as group of your elements so only apply constraints to your stackview.
Rule of thumb is to use Stack views and then use auto layout. Just put the Username and the text field in a stack view (horizontal stack view), same for the other) and then apply necessary spacing and/or autolayout constraints.
You need to learn how to work with Autolayout (I read you just started) and it could be hard concept to grasp at first (it was for me at least) but with practice, you'll master it.
If you do not use the stack view, you need to learn auto layout and size class for adapting the screen.
auto layout began in iOS 6, sizeclass began in iOS 8.
Here is the official apple documentation, to study it. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/

Xcode 6 Storyboard can't center

I have an issue trying to center an object in a storyboard. I've created a new "Single-view Application" project, entered a main.Storyboard and I'm trying to add a label to center of it. On a preview of this storyboard for 4-inch iPhone, the label isn't centered, even when I force storyboard to use 3.5, 4, or 4.7-inch iPhones. Adding constraints makes no difference. What am I doing wrong?
If you want to center the label vertically and horizontally, you can use Align option at the storyboard. On your storyboard's bottom right corner, there are four options and the first one is Align option, selecting it will give you to choose the alignment for your object as follows,
Here in the screenshot, you can see that I have selected two options if I want to center my object in both dimension. You can choose either one of them or both based on your requirements and press Add 2 constraints button and it will work consistently for any sized screen. However make sure to select the object you want to center before selecting the Align option.
Try using the reset to suggested constraints. It works quite well for simple layout. Otherwise, you just have to fine tune your constraints

Set fixed space between 2 views in Interface Builder

I am trying to learn to use layout constraints in interface builder but I am running across an issue...
I've got two views, one UILabel and one UIImageView, and I'd like a layout as follows:
the label should be centered in the superview
the imageview should be a set distance to the left of the label
Seems pretty simple, but I can't figure out how to impose a constraint for the second condition (the first is done automatically). Anyone know how to do this?
Thanks!
Click on both views while holding the command key so they're both highlighted. Then click on the pin button in the bottom right hand corner of IB (the middle button). Select horizontal spacing and the constraint should now be added. You should be able to adjust it as needed from there.

Resources