I'm trying to format a home page of my app and want the distance in the middle of two buttons to stay the same while keeping them centred on the screen using alignment in Xcode. I've been playing around with the constraints forever and can't seem to get it to work. Currently, when on a smaller screen, it seems to favourite the right side editing before the left.
Current alignment settings:
What it looks like on small screen:
I’ve been playing around with the alignment setting forever.
Refer this video
Use stackview and it will make your life easy, i have center aligned the stackview. You can give constrains as per your need.
Related
I am trying to create an Android-esque Snackbar. All the behaviour is correct but I am left with a layout issue. I have actually broken this out into a test app to simplify things a bit.
The bar is pinned to the bottom of the view and contains an Icon (UIImageView) along with some text (UILabel). This text can be up to 2 lines max.
For example, it should be able to exand out like this:-
However to further complicate things, i need it so that the icon and text are centred within the red bar and then spread out from the centre until such point that it needs to wrap. Note that there is a padding to the start of the icon and end of the text to prevent it touching the sides. An example of centred content would be..
I haven't been able to get the correct layout. I think the issue has been trying to centre the two items whilst simultaneously conforming to a width that doesn't exceed the edge bounds, causing a constraint conflict. I have also tried embedding the icon and label in a horizontal stack view but couldn't seem to find the correct fill option whilst centring everything.
I have even tried using NSAttributed string and adding the image to the text itself but when the text wrapped, the icon was being resized and/or misplaced.
As you can see, i have been doing this in Storyboard but i am more than happy to do this programmatically if it serves this purpose better.
Created this Demo for reference , it's a simple task of making a nested view inside the red view with a centerX constraint of priority 1000 and a leading constraint with 999 priority
Now you have this effect
I've been struggling to figure this out. In my app, my users can advertise their channels by adding social network addresses to their account. During viewDidLoad() I download user data and determine which social network button should be shown for this respective account. All the different buttons such as Facebook, Instagram, Twitter, Pinterest add website should be side by side, however, if the user does not have entered a specific URL that respective button should not be shown. So I'm looking for a behavior like this:
If only 1 button(fx website button) then the website button should be centered in the view. If the user has two button then the view should distribute the two views side by side around the center. And so forth for the third, fourth and fifth button. I thought I could use a UIStackview, to add the first button and have it centered but the UIStackView stretch a single view and if there are two buttons in my UIStackView the are not aligned around center instead they have lots of spacing. Thanks a lot to anyone who has any idea of how to accomplish this.
I guess a parallel could be drawn to the game Heartstone to anyone who has experienced it or want to get a visual idea of what I am thinking. When a minion is dropped on the board it aligns with minions already there.
Edit* Added picture for visibillity:
Trials:
I have now followed your advice and set the stackview to the appropriate alignment and distribution, and image aspect fit including the overall constraints. The result is far better but, but the spacing is huge they should hug with little spacing around center and large spacing to the sides see img:
overall constraints:
Kinds regards,
Can be achived by following:
use stack view
fill equally
constant width to at least one item
stackview should allign center Horizontally in container with no leeding and trailing constraints
stackview should have fixed bottom constarint
Cheer
Use UIStackView and set property of "Equal Equally" may work.
I seem to be having trouble aligning elements. I want this UITextField to always be center horizontally, and stay towards the top of the app.
This is what it looks like on the storyboard:
As you can see, there are constraints set - which are blue lines (I think that means they are correct)
However when I view it in the simulator:
These are the settings in the Xcode pin popup:
Question:
How do I get the UITextField to always stay center horizontally, regardless of device or orientation?
Edit 1:
I took out all constraints, I made the UILabel the full width of the view, and centered the text inside. I assumed that this would make it center, but alas not.
Storyboard:
Simulator:
Edit 2:
See the two red squares, I'm not sure how to change that text. There are no options on edit.
I also cant see how to add the other constraints that you suggested.
remove your left and right constraints and set the following one.
I am trying to create a simple UI that works on all devices (obviously) and I haven't had much success. The program consists of two labels, a button, an image view and a textfield, I am also using a universal storyboard.
I implemented a top constraint, horizontally centred and fixed the width and height for all labels, buttons and textfields. The only exception I made with the image view was that instead of implementing a top constraint, I used a bottom one instead as I have found that utilising the former would result in it being only partially displayed. I would then switch from the universal storyboard to one with base values. From their I would delete the existing constraints and add new ones after moving the UI elements to their new locations on the different sized screen.
When I run my app on an iPhone 4S, the layout, whilst mostly correct, is still not perfect (i.e. a label is far too close to the image). Does anyone know how I can make my layouts look correct? I have been following this guide, Adaptive Layout Tutorial in iOS 9.
Thanks so much for your help!
[Example of the constraints for the picture1
I am not sure how familiar are you with autolayout, therfore I'd suggest you watch Stanford university lecture regarding autolayout.
In short, according to the lecture and after looking at your picture, you should almost never set constraints with actual numbers. Use "Standart value" and when you can't choose "Standart value" write 0.
I'd recommend watching the above lecture and the rest of the examples in there.
Your label has 2 problematic constraints
1. Top space (30 points) to "how old is your dog"
2. Top space (28 points) to button
That means that your button is 2 points height (really small!!)
Or - because the button has already a fixed height, the label and the text field are too close (and maybe even overlaps the button)
You should delete the top space constrain (to "How old is your dog") and do something else, or give it more points height
Good luck!
I have previously worked in Windows phone and see that every control in windows phone has an Auto property, meaning occupy the size of the content.
I see that in iOS such a property does not exist. When there are dynamic data to be bound to a UILabel, I always need to calculate the height of the data and then assign to the UILabel. This takes a good amount of time and bit painful. Is not there an Auto property or am I missing anything here?
iOS has AutoLayout which is really helpful, get familiar with it.
Click on the Label
Click on the pin constraints button (little square button)
Add your custom LEFT, RIGHT, TOP margins or LEFT, RIGHT, BOTTOM margins
Click on "Add 3 Constraints"
Set number of Lines to 0 which means as much lines as view needs
Then you probably got warning lines, but you can solve them
Just click on fix constraints button (little triangle button)
Click update frames
UPDATE
Important: the answer to your question is to PUT NUMBER OF LINES TO 0 you can use that UILabel with 0 lines(which is autosizing) with frames and AutoLayout. AutoLayout is just a friendly suggestion that can be helful to setup views. Also put Line Breaking Word Wrap
Here you go also with some useful links for working with AutoLayout. AutoLayout is great because you don't care anymore what size is the screen, what orientation has the device at that moment. You just need to setup everything correctly and everything works amazingly but if your setup is wrong then AutoLayout might become your enemy. So start learning and experiencing right now.
Very good point to Begin learning AutoLayout
If Your are being lazy, start from video tutorial series
Great iOS7+ table view tutorial with autoresizing cells
Also check out this Stack Overflow discussion
You need to familiarize yourself with Auto Layout:
Auto Layout dynamically calculates the size and position of all the
views in your view hierarchy, based on constraints placed on those
views.
Just give top, left and right constraints and make label's numberOfLines to 0. That's it. Label's height will resize automatically.