r/iOSProgramming • u/[deleted] • Oct 22 '18
Interface Builder + Auto layout is such a tedious system for designing views
Seriously it is harder than writing the code for the app!
To begin with interface builder doesn't provides even the most rudimentary design/layout tools that you would expect from such a system. It also behaves in the most unexpected ways: select an item in the Document Inspector you would expect that you can then move it around with the arrow keys, but nope, it starts selecting other items in the list instead. You want to move and item on top of another but don't want to place it inside of it, well, have to do it manually with the arrow keys or changing the x/y value can't just drag it. Selecting specific items from the view (say you want to select the stackview itself and not one of it's items) is also very troublesome It is such a weird system.
Auto layout is just insane. I spent nearly an hour trying to figure out why a vertical stackview that I had with labels simply refused to expand it's width when I pinned it 16 p from the left and the right. For some insane reason a subview of another view (which I couldn't see happening at first due to it having no background color) that the stackview was not a part of was shrinking to the current width of the stackview when I added constrains to the stackview and was thus preventing the stackveiw from expanding to the desired width. You apply constraints to one item and the thing then adds a bunch of constrains to other items making it hard to keep track what exactly you are doing and then it shows you a bunch of errors/warnings with suggestions like "Add missing constrains" which if you use will only make your layout worst.
What a cumbersome system. Stuff like HTML/CSS, Android with it's XML or even React Native with it's XML/CSS like system are so much easier and intuitive for layout views. Trying to do anything beyond having more than a couple of items in Interface Builder + Auto layout is an exercise in frustration.
4
u/jontelang Oct 22 '18
The editor do behave quite interesting in many cases where you expect it to behave nicely. But IB+AL isn't actually that bad, but yeah sometimes it has some less than obvious cases to work around. Are you new at it though?
1
Oct 22 '18
Are you new at it though?
Yeah, have done a couple of apps already but the IB+AL part to try and make the app adapt to the different sizes is really tedious.
4
u/jontelang Oct 22 '18
I suppose I haven't really done a lot of apps where device size required vastly different layouts.
-4
Oct 22 '18
Neither have I, only iPad / iPhone layout and I use a separate storyboard for each.
Setting the constrains themselves even if it is just for one layout is still really tedious.
18
u/David_Edward_King Swift Oct 22 '18
I'm going to put it this way; have you ever tried creating a screen that looks good across multiple devices programatically? That's hard, no matter what you build it with. You talk about CSS, but even there you need to write a lot of separate rules to make sure the website looks good across multiple screen sizes. It might be more intuitive for you, because you might have experience with web development, but in my opinion iOS's constraints are really as intuitive as it gets. Android's XML is great too, but still can't compare to how useful AutoLayout is in my opinion.
3
u/iindigo Oct 22 '18
I agree, as a dev whose work is 99% iOS or macOS native, CSS and flexbox can really drive me up a tree sometimes. In those situations I’ve caught myself thinking, “if only I had autolayout I’d be done already!”, which may have been true but the reality is that I’m just more accustomed to autolayout.
3
u/David_Edward_King Swift Oct 22 '18
That’s funny, flexbox is actually something I love about css (or NativeScript), but I do agree it’s something that needs some time to wrap your head around it. Give flexboxfroggy a try if you haven’t yet!
17
u/NSBoss Objective-C / Swift Oct 22 '18
Unpopular opinion: I think Interface Builder is great and i really enjoy using it. I’ve also had similar issues like the ones you described and I’ve learned what to do and what not to in order to get the job done without a hassle. I think it’s a matter of mindset.
3
u/mikebld Oct 22 '18
not so unpopular
5
u/NSBoss Objective-C / Swift Oct 22 '18
I don’t know, i keep seeing people banging their chest about doing UI in code only. I can see the limitations but in my opinion it doesn’t justify ditching this tool entirely
8
u/LatinBeef Oct 22 '18
Honestly IB+AL is pretty great. With more experience you’ll see its value.
Here are some tips:
In general, each object needs a minimum of 4 constraints
You can set a left, right, and top constraint on a label and the number of lines to 0, and its height will auto adjust to the content.
Build the layout and constraints one object at a time. I like to create a mock up of the design first and work from that.
“Group” parts of a view by adding them in a “container “ UI View. The constraints on the objects inside will remain if you need to change the layout of the entire view later.
I remember learning auto layout and thinking it was tedious as well, but honestly that was an ego response and me not wanting to take the time to learn it. Take the time to learn it yourself and you’ll see its value.
8
u/europeanwizard Oct 22 '18
How about programmatically layout out your views? On some projects I've done, the client required we not use Interface Builder, but instead add all views and constraints by code. It's a bit more precise, and once you get the hang of it, you'll fall into a rhythm.
Some advantages:
Views can be set to private let
Buttons and stackviews sometimes only need to be declared in viewDidLoad()
Changes to constraints are very visible in version control
You can set up constraints using VFL, using anchors or by creating NSLayoutConstraint objects. You'll have to try out and see what works for you.
I found it most useful to create a list of anchors, and then add them at once, like this:
class FancyViewController {
private let stackView = UIStackView()
private let tableView = UITableView()
// ...
override public func viewDidLoad() {
// ...
let guide = self.view.safeAreaLayoutGuide
let constraints = [
stackView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
stackView.topAnchor.constraint(equalTo: guide.topAnchor),
guide.trailingAnchor.constraint(equalTo: stackView.trailingAnchor),
guide.bottomAnchor.constraint(equalTo: stackView.bottomAnchor)
]
self.view.addConstraints(constraints)
}
}In older code I used VFL more, like so:
override func viewDidLoad() {
// ...
let viewDict = ["tableView": self.tableView]
let viewLayouts = ["H:|[tableView]|", "V:|[tableView]|"]
let viewDict = ["tableView": self.tableView]
let viewLayouts = [
"H:|[tableView]|",
"V:|[tableView]|"
]
for layout in viewLayouts {
self.view.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: layout,
options: [],
metrics: nil,
views: viewDict))
}
}The disadvantages of doing everything in code are obvious, of course. You loose the advantages of seeing everything in Interface Builder, and it's slower to type everything. Plus, your view controllers tend to fatten up with repetitive code.
6
Oct 22 '18
Plus, your view controllers tend to fatten up with repetitive code.
You are supposed to put the view code in a UIView subclass and just instantiate it in loadView instead of littering your VC with view code.
2
4
u/KarlJay001 Oct 22 '18
The reality is that we have several different screen sizes and everyone has portrait and landscape. You also now have multi-tasking on some that will change the screen size.
If you try to do everything without auto layout and the size class, you'll probably have even more frustration.
It would be interesting to try and do all the sizes for all the devices without it just to see if there's a better way.
Remember, EVERYTHING you do in IB can be done in code.
I even suggested that the all of IB is in XML so you can read it and convert it to code, but that Idea was shot down.
I'll admit that it's not the funnest thing in the world to learn, but the reality is that with all the different sizes out there, it's not going to be easy to address all the issues.
3
u/canpoyrazoglu Objective-C Oct 22 '18
Disagree. I use Interface Builder + Autolayout for almost all the views when possible, and while it has some quirks, it works great! I think it’s MUCH better than writing code and you can do a lot with it when you truly understand all those layout priorities, multipliers, and compression resistance/hugging priorities in combination with IBInspectable and IB Designables in custom views.
I think you should give it some time. You’ll love it. Seriously.
2
Oct 22 '18
For the width issue you’re having, you may want to look into Auto Layout’s “content hugging” and “compression resistance” priorities. Especially with stackView’s, I find myself fixing issues using those far more often than adding or removing constraints.
2
u/CaptainQuirk336 Oct 22 '18
Hold Ctrl+Shift and click anywhere in a group of views to select a particular view.
2
Oct 22 '18
Quality UI implementation in iOS has a rather steep learning curve. The lifecycle of viewcontrollers is another example.
-3
u/Notsileous Oct 22 '18
I am with OP, there are a lot of people who really dont mind it, I assume most started with iOS and use it gauge everything else. Coming from Android its an absolute nightmare, you are used to putting something in a spot and not having the interface move it to the opposite side for no reason.
I have gotten more used to it over time, I can still build an Android interface in half the time as iOS but I also dont spend the hours that I used to dealing with it. For me, what helped me the most was:
A: Not using stack views, they behave too strangely
B: Not using auto layout, I would rather spend the time setting things myself than fixing the auto layout nonsense.
40
u/[deleted] Oct 22 '18
I would disagree - but this is obviously subjective opinion. I think that’s it’s a really easy and fast way to get a view up and running. It definitely is a change in how you think and you do have to get into its mindset because it can be finicky.