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.
Related
I've run into a problem with UIButtons in Stack Views. The image that the buttons are set to get squished whenever there's not enough vertical space for them, such as when the phone is in horizontal orientation, or on a small device.
I did find a solution, by running: button.imageView?.contentMode = .scaleAspectFit in viewDidLoad() but it seems like the wrong way to do it.
I assume that the best way to do this would be within Storyboard. First of all, is this the best way? Second of all, how would I do it? (I'm still very new to iOS development)
I have tried using in the attributes inspector, but that doesn't seem to have done anything. Maybe I'm missing a step.
Storyboard vs Code
There are people that prefer code over storyboard. There are people that prefer storyboard over code. There is no wrong or right way since both ways are correct. Do whatever you think is better and/or easier.
Setting the Content Mode through the Storyboard
I don't think this is possible. I think the Content Mode you were trying to set is the Content Mode of the button you had selected. I don't think there is another Content Mode dropdown for the embedded UIImageView in the UIButton. Your best bet would be to set the .imageView.contentMode through code like you already did.
I'm working on an app where I've got a design with two similar layouts for horizontal/vertical. And it's on an iPad. I've found other answers but I can't use most of them because I've got two tableviews in my view, which means I can't use xibs. I'm also making it for the iPad so changing layout based on height/width variation doesn't work.
The design I'm trying to achieve is in the pictures below. What I'm having troubles with is moving the two views that are stacked on the right in landscape mode to be next to each other and short in vertial. Also I need to move those four buttons and the right-side table to being below the other two views.
I've tried overriding trait functions as described in this blog post but for the life of me can not get it to work.
I'm looking for any advice anyone could give me. I feel like I've hit a wall and can not figure out how I can implement this.
just give the height and width constraints of the subviews with respect to the superview in ratio it'll work.
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.
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...
Storyboards are an awesome feature - but is there a possibility to create a lanscape and a portrait view for one controller? What is the best way to do that?
I found a lot on StackOverflow, from copying a storyboard to add subviews for each orientation. It would be great, if there is a better way to do that...
Autosizing works only on simplest straighforward cases. If you have to tweak just a little bit the end result, it becomes too hard. What I do to make this much more visual is I set autosizing, then duplicate the portrait view right for the same controller in the same xib and rotate it to landscae. Then I adjust the positions. But before that I set tag properties for each control. So I create actually 2 duplicate views, 1 portrait and 1 landscape and since the tags are the same, during "willRotateTo..." or "didRotateTo.." I copy the .frame property of each control from the duplicate View that I visually adjusted in xib, and that puts the control where I wanted.
I can even add animation along the way.
You want to use autosizing of subviews for each UIViewController of your storyboard. It's the cleanest way of managing rotation.
Check: http://www.techotopia.com/index.php/IOS_5_iPhone_Rotation,_View_Resizing_and_Layout_Handling
You aren't supposed to create multiple scenes for every orientation. You're supposed to use autoresizing, springs and struts, custom implementations of several rotation methods and respond to notifications.