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.
Related
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.
I am trying to accomplish what the Imgur app has done in one of their UICollectionViewCells in their about view. See below:
I want 1 UICollectionViewCell but instead of 3 sections of views inside the cell, I just want two (comments and posts).
For my UICollectionView I am resizing the cell at runtime to make it fit almost the whole screen minus a bit for margin's sake because I want to make a card view like Imgur.
I followed this stackoverflow post to get 2 views side by side: iOS Autolayout: two buttons of equal width, side by side
and I am able to get 2 views side by side of equal width. Here's what they look like in the storyboard:
but when I run the app, my views get stretched because of the autoresizing. See below:
How can I get the views to both be of equal width and have equal spacing between, before and after the views?
You can use UIStackView. It provides an easy way to lay out a series of views horizontally or vertically.
Select the two views and click on the new Stack button in the Auto Layout toolbar at the bottom right of the storyboard canvas:
Give constraints to your stack view from top, left, right and bottom. Then make your Attributes Inspector of the stack view like this:
NB. If you want to learn more about Stack View, follow this fantastic tutorial in Ray Wenderlich: https://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views
Believe me most of the complexities associated with Autolayout will become trivial once you learned Stack View
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/
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 am trying to add two buttons to a view that also contains a table view. I want the two buttons to fill the width of the screen for different screen sizes (portrait only). There is a minimum width of say 100 units for the buttons to hold the text, but I would like them to be wider for larger screens. I would like the space between buttons to be at least 20 units and at least 20 units of space between the outside edges. This seems like it should be straight forward, but I can't make sense of the align and pin constraints in the Storyboard auto layout. So far, the tutorials I have seen are confusing and don't seem to cover the right amount of detail to help me. Please provide me with some guidance. Below is the layout before constraints are put in place.
And here is what the layout would look on a 4" iPhone:
I have already spent a day trying different combinations of constraints, but continue to have problems. Just when I think I have something that works, it won't build because it says I have conflicts.
I am trying to use the following tools (along with the align tools - not shown) to create the proper constraints. I have tried selecting one or both buttons and then using the editor to set the constraints, but each attempt looks bad. Even when I choose "Reset to Suggested Constraints", I get wacko results. Please help with suggestions on how to accomplish this seemingly basic task.
You must have:
Leading for first button 20
Trailing for second button 20
horizontal spacing between buttons 20
equal widths for both buttons
width >= 150 for each button