Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you right align a horizontal UIStackView?

I've yet to find an answer for this anywhere and I'm not sure if it's possible, but I'm trying to right align a horizontal UIStackView, so that if subviews are hidden they move towards the right side not the left. Either programmatically (in Swift) or using the Interface Builder

like image 466
Programmer Unextraordinair Avatar asked Oct 25 '16 08:10

Programmer Unextraordinair


2 Answers

UIStackViews align according to the user's text direction, i.e. left aligned for English and other Roman script languages, or right aligned for languages such as Hebrew.

In my opinion, changing this for layout reasons may be a misuse of the text direction APIs, and a bit of a hack, but with that in mind:

You can change the direction for a particular view in interface builder, using the Semantic drop down, which has options for 'Force Left-to-Right' and 'Force Right-to-Left', which will change the direction they pop to but also the order they are shown in. So you will have to reverse the order of the elements in your stack view

Semantic selector in IB

Or you can do it in code, using the view's semanticContentAttribute

stackView.semanticContentAttribute = .forceRightToLeft 
like image 86
Josh Heald Avatar answered Sep 19 '22 08:09

Josh Heald


What worked best for me was putting my horizontal stackview inside a vertical stackview and set the vertical one's alignment to leading

like image 38
Rachel Harvey Avatar answered Sep 19 '22 08:09

Rachel Harvey