How to place "spacer view"? - ios

I've read quite a bit about spacer views - they seem very handy in utilising all this auto layout stuff.
I understand all of the concepts behind them, that's fine... I'm looking to practice with them now but before I could even get started I've found myself getting stuck on a more fundamental issue that's really doing my head in... maybe I'm too tired.
How do I actually place a spacer view? I'm looking for some drag and drop, native kind of thingy... but I can't see any "spacer view" in the object browser.. do I have to create this view myself? Where does it come from?

Spacer views are just standard UIViews.
If you want to make them easier to use in IB then give them a coloured background and make them visible but in the runtime attributes give them a Boolean...
Hidden = YES;
That will hide them in the actual app.
The reason they are used with auto layout is because you can't create equal spaces. A space is the gap between two objects. It isn't an object itself so you can make two spaces equal (for example).
But you can replace the spaces by instances of UIView and now you can give these UIViews equal widths or heights. Make them hidden and the constraints still act on them but they cannot be seen.

There's a simple "UIView" widget you can drag and drop from the lower-right corner area of Interface Builder. I don't believe there's any official "spacer view" terminology in Apple's kit...

Related

swift 4 and Xcode autoLayout and UIImageview as Button

I am working on auto layout and making those UIIMages as Button.
I would like to make those button to be clicked and switch the images
as well as showing some words in the label.
In this case what should I do?
In addition, as it is shown, the auto layout ratio is different only from the iPhone SE. In. this case What should I consider?
Many thanks,
The first thing you need to learn is auto layout. Start with basic Always go with Apple's doc first
This one I likes he is good and you will also find how to use buttons as image view, using card game example, check other tutorials also very helpfull.
Stanford university autolayout by Michel Deiman
As far, your question just gives your label Horizontal in the container constraints(open storyboard in the right bottom third from last (allin) click on it popUp will open), and 20 bottom to the buttons and it will be in the centre. Every elements need x,y height and width so it knows exactly where to place, so you also need to give constraints to the buttons, Learn stackView to understand how to place your button without giving many constraints to each button and stack view will handle it for you, try hide and show to one of the button and magically stack view will adjust all adjustment. I suggest before staring Do read apple's doc first.
And Your question is too broad to answer, so read and try adding different constraints based on your need and ask a specific question(after trying by yourself) like why this label getting cut what constraints should i add so people know exactly what you want to achieve, people are always ready to help, just ask the right question.
happyCoding

Best approach to showing or hiding dynamic content in iOS

I've been doing iOS for a while now, but when it comes to dynamically hiding / showing elements, I'm a bit lost.
Coming from Android, I'm used to being able to simply set views to visibility gone, but this doesn't exist on iOS.
So let's say I have the following scenario:
Basically I want to have a table, but the table should not fill the entire view controller. Instead it should leave place for optionally either a button, a multiline label, or possibly both at the bottom (if visible, these should be fixed, not scroll).
One way to solve this would be to use auto layout and modify constraints, like adding a zero height constraint. But that would make iOS kill one of the other constraints, which would make it hard to change it again. For the label, I wouldn't always want to have a height constraint, because it could be multiline, and should take the size it needs.
Maybe it's easier to skip autolayout here and modify frames instead, I don't know.
My question is: What approach would be best here?
Is there some other way of doing this I haven't thought of, or do I have to try to do what I described above?
I'm not primarily looking for code (code can be ok), but I'm more interested in a description of how it can be done.
I'd like to support iOS 7.
This problem had a variety of solutions, and opinion based, but I'm facing such questions a lot, when I don't know what to choose and what would be the "right thing".
So, I my opinion, the best solution here is using autolayout, you need to set height of label manually, but you have a few methods for this, at least you can play with it and if you don't succeed ask question about it. Using frames, you'll face same problem of calculating height, right? But with auto layout, you only need to set height, vertical space to 0, when you need to hide message.
You can also use constrains with priority lower 1000, and remove completely constraints from message (button, label) if you don't need it at all anymore.
For example, taking your layout image, you can make UIView with subviews: button, label. Top constraint connect to the UITableView, other constraints to the sides.Label and button will calculate the view's height. The only question here is label height.
So in ios assuming that the background of both these objects is opaque only the front most view in the Heirarchy will be visible and interactable, An easy solution would be to change the different frames of these two things you need and make sure they are in the back of your view heirachy, and when you need them to appear use view.bringSubviewToFront(mySubview) and view.pushSubviewToBack(mySubview) to make it disappear again. View obviously would be referring to main view of your view controller.

Embedding lots of views into a uiscrollview and have constraints match up? Is this possible?

So, here's the basic idea. I have a view with tons of subviews, they all use AutoLayout. I need them all to scroll up to be seen, so I go to embed in uiscrollview and... everything is off. This app isn't using size classes, just one Any Any type.
I went to embed in a view instead and also, the constraints do not adjust for the new view, and its 20 extra width/height in pixels.
I'm betting this is like Scrolling Right on your air to view asset catalog imgs, it's simply impossible, but wanted to ask. If it is impossible, shouldn't this be a bug report. I'm used to embedding, then editing, and tearing down/rebuilding xibs, but with all the hype around it, I keep thinking this new feature should make it easier, not harder, so I think I'm missing something basically.
I did search and there's lots of questions from people who haven't embedded anything before, but I didn't see anything for 2 or so views already being in the new embed + NSLayoutConstraints now getting completely wiped, it's relatively complex so me, not knowing the design, has to go memorize all 40 or so relations and remake it, or maybe just trust the "set frames to new view", but that probably won't turn out right since I have to make the view 20 pixels smaller each side, to counterbalance embeds
Thanks for any help. Shortcuts, tools welcome.
I figured this out. I was like "Well, I'll just embed the main View Controller's view into a subview", but nope, that's not allowed.
So I copied and pasted the head view into itself, deleted the originals, then embedded that one-layer-deep into a UIScrollView and manually set the size back and origin back to 40 pixels less each side, and 0,0 for x, y.
I have to re-hook up all the outlets but this is much easier than anticipated.
As a side note, it's still impossible to view [right side of] asset catalog on an air (without toggling off all sidebars), but it's not impossible to quickly do keyboard add/edits.
To answer the question, yes its possible. Use autolayout with scrollviews you need to have a content view set inside the scroll view which has the same size and width of the scrollview. With constraints bounding its size left, right, top and bottom.
Then add the subviews to the content view. This will make the scrollview height much easier to manage.

Problems with autolayout and resizable views

I have issue with autolayout.
I have three different views (could be n views as well). When I pin first one to top of super view and connect one next to the other everything seems fine. But if I want to change height of first one the other two does not follow (they stay on the same position). What I find weird about this is that if I connect only two views this works perfectly.
To have better understanding what I am trying to achieve I made simple, example solution with three views:
http://i.stack.imgur.com/cMwiB.png
So, when I set height of green view to 0 ( can be any other number as well ) and only red view is connected, this works fine, red will move as it should. Blue one is not connected and it remains where it is:
Here is screenshot when I connect blue to red:
What I tried to do:
connect blue one to green as well ( not working)
connect blue only to green ( not working )
"boxing": have boxes with views at the time ( ugly, hard to manage, not working in all cases)
What I would like to know:
what makes this happen? Can I change this kind of behavior?
What is the best practice to have flexible length for height (Besides UITableView)
Thanks in advance.
Ok, I think you're falling into the same trap I did, when I investigated autolayout some time ago. It's tricky to explain, but I'll do my best. When you work with autolayouts, you need to forget completely about setting a view's frame at all. And that, my friend, includes changing size and positioning. When you use autolayout, you define some constraints, some "rules", that the view tries to respect when rendering the screen, so the more specific the rules are, the less random will be the behavior of the view. I mean, if you just define the spacing between views, you're implying that the height of the views doesn't matter, so it's possible that some views grow or shrink when the re-layout is called (that is, if you don't specify a constraint for size. You probably want to always specify the size of some views in every layout...)
By the way, you're allowed to violate the constraints by manually changing the frame of an element AFTER a re-layout is called, but when the re-layout method is called again, the constraints will be forced so the size and positioning of the views will change. Quick tip: an easy way to force the re-layout method is to change orientation (command+left / command+right in the simulator).
So after saying that, I have to say that the layout you provided is completely working as intended, at least to me. When you change orientations the constraints you specified are ALWAYS being respected. If you want, you can try to apply some frame-setting in the viewDidAppear method, because this one is called AFTER the autolayout (and thus, you're able to violate the constraints temporally). After doing this, your view will be broken but once you change orientation the constraints should be respected again. Make sure it's that way.
From here I can only wish you luck ;) Oh, and refer to the documentation, it was a life saver to me when I looked at this half a year ago. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html It might seem a classic, but it's a pretty nice doc.
EDIT: one last thing. I have the impression that constraints are not designed to be dynamic. You define them once, and they're there always. Their main purpose is to ensure that subviews are rendered as intended in every screen resolution without fail, so if I were to make an application with moving views, I would leave them outside of the autolayout, or avoid using it at all. Just a personal impression though :)

How can I manage UILabel's position when rotating the device?

I'm using storyboard.
And I have a toolbar with toolbarButtonItems separated by flexible spaces.
I want labels for each buttons. So I put labels in storyboard, but when rotating the device to landscape, labels are not aligned with buttons.
How can I solve this?
Any suggestions are welcome.
Kindest regards,
Well good sir, like many before you, you're presented with a two options.
Use Auto-Layout's Constraint system to describe what you want.
Use -layoutSubviews, and manually reposition the views in your ViewController.
The Second document on is on how to subclass UIView's in general. Defiantly worth the read. Since you said you're a beginner, it's probably best to use Auto-Layout, so you should probably start there.

Resources