I'm fairly new to iOS development but have reached the point where I want to create my own composite UIView as a custom UIButton. I would like to layout a UILabel and 2x UIImageViews as follows;
But I would also like to anchor the controls (subviews) in such a way that as the label expands, due to translations say, the view automatically handles the extra real estate. For example;
Ideally -
- the right hand side UIView is anchored to the right; and remains a fixed width/hight (right aligned).
- the label and bottom image divide the remaining left hand space
- the label is vertically centered in the top half of the upper remaining space
- the bottom image remains centered (both vertically and horizontally) in the lower remaining space
- if the label is wider than the bottom image then the view should expand
I'm happy to construct this in pure code if required. I used a XIB in the above images to play with attributes and to visualize my question.
I'm from a C#/XAML background so I would typically use grid layouts with fixed/auto/* columns and rows but I'm guessing I need to use something like NSLayoutConstraints here - unfortunately I don't know where to start or how to search for the solution. Any help appreciated!
in your UIButton
(code not tested)
Et voilà !
The method is always the same : you need at least 4 constraints on each view, setting width, height, x, and y (CGRect 4 dimensions). Think of a constraint as a relation :
to translate in this form
Note that using visual format, you might express all that with less code. (but I've not played with it yet).