Why is view resizing on aligning by horizontal center in container in Xcode 6? - ios

I'm trying to align Text Field in View Controller with pressing "align" and selecting Horizontal center in container but then my Text Field get resized. Same thing happened with Image View. What am I doing wrong?

Related

iOS layout: How to keep a button in the middle an image?

I have an image that needs a UITextField in the middle of it at all times. However, I can't get it do what I want.
I pinned the image with top, left and right constraint and gave it a height constraint. I put the UITextField in the middle of the image and tried various methods to keep it in the middle, but they all keep failing. Any tips are appreciated. Here is what the image currently looks like. First one iPhone 4 and second one is iPhone 6
You can just select the image view and the button and create two constraints: a “Horizontal Centers” constraint and a “Vertical Centers” constraint. Neither view has to be a subview of the other, and they don't have to be in the same superview either.
Here's a demo. I've already constrained the image view to fill the top left quadrant of the root view, and I've dragged in a button. In the demo I'll select the image view and the button (hold shift to select multiple views) and create the two centering constraints. Then I'll use the Preview assistant to show that the button stays centered when the image view's frame changes.
What you need to do:
Embed the UIImageView and UITextField inside a UIView.
Set constraints of UIImageView leading, trailing, top, and bottom to UIView with constant of 0.
Set constraints of UITextField center horizontally with container view, and center vertically with container view.
Then it's up to you on where you want to position that UIView. This will definitely always position the UITextField to the center of UIImageView.

How to Re-Size Label to fit any screen size

Ok, here's what I'm trying to do:
I would like to create a scroll view to scroll vertically, in case my text is longer than the screen size (think iPhone 4 vs. iPhone 6).
I've added a scroll view and placed a view within that. I embedded by label for the text within the view.
However, the scroll view currently will scroll only horizontally.
Is there a way to constrain the label width to be the width of the screen? How do I do this?
You can constrain the width to contentview to be equal to scrollview width. And pin the four sides of label to the content view.
Also you will need to pin all sides of content view to scrollview, if you haven't already did that.
If you push the constraint button on the bottom of your storyboard pin it to both the left and right side. Uncheck constraint to margins and pin it both 0. :)

Center align a view vertically to other (sibling) view iOS

i am working on an iOS project and want my 2 views to be vertically aligned with respect to each other as shown in attached image.
There is an image view on left and a view on right side. The right side view contains some labels in it. I am using autolayouts and have properly applied constraints to image view (i.e. top/leading/height/width).
Now i want my right view to be center aligned (vertically) to thumbnail but when i do this, it asks for missing constraint y position. I don't know how to fix that (i can not add top/bottom constraint as my title label can have 1 or 2 lined text and the right side view will grow accordingly)
Is it possible to achieve this? if yes, then how? Thanks.
It's a tableView basically. Above image is for detailed description. Whereas, design should be like the following (right side view should be centered aligned to left side image)

Constraints inside Scroll View

I'm working on application for iOS. Trying to use Autolayout with Scroll View.
But there is some problem. I have View -> Scroll View -> Content View hierarchy. I pinned (top, bottom, left, right) of the Scroll View to the Main View, and also pinned Content View to Scroll View.
Then i've added imageView on the top of the screen and text label below, to the Content View. Added them constraints including vertical spacing (8pt) from image to label. In Preview everything looks ok and label placed right below image, however when i'm running the app on iOS Simulator, my label places right in the middle of image. Please, any help?
We found an answer in the comments: it was due to a wrong content scaling mode of the image. It needed to be ClipToBounds.

Xcode 6: How to make an image view fill the screen on all devices? Auto layout not working?

So I have an image view and a button. I need the image view to be the correct size (not look squished or stretched) on all devices and more importantly I need it to be the size of the screen. I need the button to stay in about the same place on all devices.
Back with Xcode 5 I remember auto layout fixing this problem but now that does not work. I looked at https://www.youtube.com/watch?v=G53PuA_TlXk and tried control-dragging to the view controller and centering the view both horizontally and vertically, but I can't get it to scale to the size of the screen. The video doesn't address this.
Is there a specific option I need to choose when control-dragging to keep the image view scaled? What should I do?
You need to do 2 things:
Set up your constraints properly
Choose the type of scaling you want.
Setting Up Your Constraints:
Drag out an Image View and place it roughly in the center of your view.
Set your size class to wAny hAny.
Click on the Pin icon |-[]-| at the bottom of the screen.
Turn on all four orange I-beams. Set the four constants to zero.
Uncheck the Constrain to margins checkbox.
Click Add 4 constraints
Choosing Your Scaling:
Click on the Image View you added to your View Controller.
In the Attributes Inspector on the right, choose your image.
Set the Mode of the View to Aspect Fill (fill entire screen cropping top/bottom or left/right as necessary) or Aspect Fit (letter box image so that entire image is shown uncropped).
You can view the constraints that have been created for you. In the document outline, you should see 4 constraints under your image view:
Click on a constraint and then view them in the Attributes Inspector on the right. Here are the 4 that I created:
This is what worked for me:
View Mode:
Aspect Fill
As constraints for the image:
Align Center X to: Superview
Equal width to: Superview
The 'Equal width' property is the one that scale the image to the size of the screen.
If you want to have the button in a specific position, set constraints from it to the view, the main ones you want to consider are a top constraint and any left or right constraints. So you are telling the view where you want to position the button in relation to its sides.
Hope this helps.
Not sure I understand "exactly" what you're after but if you want to drag an image view into the controller, and that image view to automatically fill the whole view controller background: just drag the image view component into the view controller window.
Thing is: if you see a blue bounds line around the view controller, before you drag, the image view won't fill the view controller.
You need to click anywhere on the background in the view controller pane, so the blue bounds line disappears, only then, will the image view you're dragging in, fill the whole controller background automatically.
If you find, using previews, your pics are getting squished or stretched, then, just go to the "Resolve Auto Layout Issues" menu, and click "Add missing constraints" - then all should resize to look ok on all screens.
In my constraints, I needed to uncheck "relative to margin".

Resources