Adjusting Subclassed UITableViewCell UILabel Frame - ios

I have a subclass UITableViewCell. In storyboard I added some labels and buttons and created respective outlets, and I set and tweaked their respective frames using the size inspector. The layout looks good for a iPhone 6. On an iPhone 5 things are off screen on the right. In the layoutSubviews method of the UITableView subclass I attempt to adjust the frame of my labels and buttons. When the tableview first appears the buttons and labels are in the positions specified for their respective frames in Storyboard and not in the frame/position I set in layoutSubviews. A moment later they appear in the positions set in layoutSubviews. Sometimes however, I see it revert to the old storyboard set position.
Any ideas what might be happening? I want to set the labels and button frames explicitly and have them stay where I put them. I don't want to use constraints as they are really painful to use in Storyboard, and frequently don't behave well or with good granularity.

If you do not use constraints, please remove use auto lay out and use size class.

Related

Why does UIButton resize when I access titleLabel property?

I'm trying to adjust the size of a button to (it's intrinsic size + a little bit more) in order to draw a custom background. However, every time I access self.titleLabel within the button, the size and position resnaps to that of the storyboard. I don't have to do anything with the label to reproduce this, just retrieve it from the button.
I've put logging code all over my button and view controller in order to find where this is happening. It's not coming from a relaying-out of subviews or any other notification I see to get within the view controller. The line before accessing titleLabel, the position and size are correct. The line after, it has snapped back to the storyboard position. Commenting out the access prevents the size/position snapping. Can someone tell me where or why this is happening?
I have no constraints set (that I can tell), but am I fighting against auto-layout here? Should I be doing this a different way like composing controls or something?
Something similar (or the same?) has been asked before at UIButton modifying titlelabel seems to change its frame and UIButton titleLabel resizes on press?, but both were left unanswered or explained away with just "maybe a bug."
If the project has auto-layout enabled, then YES, you're fighting auto-layout. You have two choices, either subclass UIButton so that you can override the intrinsic size calculation, or modify the constraints so that the intrinsic size is not used in any constraint. If you do the latter, then you probably want to create an IBOutlet to the constraint for the width, so that you can adjust the constant property as needed.
This isn't a bug, it's a consequence of auto layout. When using auto layout, you shouldn't set any frames. Instead, you should change the size or position by modifying the constraints. What's happening, is that whenever the view needs to be redrawn, the frame reverts to the frame that's defined by the constraints.

How do you override a storyboard element/view on iOS?

I have a UITextView set in my storyboard at a certain position. When the app first loads up, I would like the textView to be at a different position then where it is in the storyboard. I do this programmatically by setting its frame to a different frame with a different origin (later the textView will be animated back to the original position). But no matter how I do it, the app always starts up with the textView in its storyboard assigned position. I can't seem to do this anywhere... viewDidLoad:, viewWillAppear:, nothing works. How can I override it?
One other thing... lets say the textView has a different origin then it does in its storyboard (the frame was reset at some point in the app). If I add a subview to the textview using addSubView:, the textView resets to its original position in the storyboard. Why?! Any way to stop this?
If you want to change the position of the textView when it starts up, I'd suggest that you do not have it in the storyboard and instead add it to the subview with its correct frame.
Especially with Autolayout in effect, having it there already means you would have to modify its constraints and if you're modifying its position programmatically already anyway, you might as well add it to the view programmatically too where you have more control over its constraints programmatically as well.
Under auto layout you can't set frames - well, you can, but as soon as another layout pass takes place, the frame will be reset to that defined by the constraints.
The simplest way to change the size or position of views you've laid out in interface builder using auto layout is to create outlets to the various constraints, and then edit their constant properties. If that doesn't give you enough flexibility, you still need outlets to the constraints, but when you want to move things you'll have to delete those constraints and add new ones.

iOS 6 Constraints Making the UIView Longer

I am not sure what is going on and how to handle this. I have a UIView in a cell and I set the constraints using IB and for some reason it stretches out to the end of the cell. How can I stop it from stretching.
And here is the result:
I am dynamically injecting a stepper control but as you can see the UIView stretches to the end of the cell. I tried pin width then it completely messes up everything and does not even display the UIView.
Here is the screenshot of constraints:
Okay, let me draw some distinctions here.
The trivial way to this is you always have the stepper in the cell (and no UIView). For those cells where we are not supposed to see the stepper, you set its hidden to YES; for those cells where we are supposed to see it, you set its hidden to NO. That is what I would do.
The programmer way to do this is you don't have any blue UIView in your cell at all. You add the stepper in code, and you also provide its constraints in code to position it. You said:
so I don't have to hardcode the coordinates
But adding constraints in code is not "hard coding" the coordinates. With constraints, you describe the position conceptually in terms of insets from the superview, and as the superview resizes, you are still correctly positioned. So there is no philosophical objection to adding the stepper and its constraints, and it is just laziness not to do that.
The lazy but wasteful way is to do what you are doing, i.e. add the blue UIView in the nib as a guide. Even if you do use the blue UIView as a guide, though, you should be able to get this right if your constraints are right. I just tried this and I was not able to reproduce the problem: my blue view appeared in my cells in the same place where it appears in Interface Builder.
The instant solution is don't use Autolayout in the nib from which a UITableViewCell is drawn — i.e. uncheck "Use autolayout" in the file inspector for this nib. You will thus use old-fashioned springs-and-struts (autoresizing) to position the subviews.

Why is AutoLayout not taking care of my UICollectionView

I have built a very simple sample of an app (Source code on github) using a UICollectionView.
Everything is working fine, as long as the app is in portrait mode. When it is changed to landscape mode however, the content cell is not resized appropriately, and thus, nothing is displayed.
I thought that all the necessary AutoLayout constraints are in place. I am aware that I can implement collectionView:layout:sizeForItemAtIndexPath:, but my goal is to use AutoLayout as much as possible (simply to understand AutoLayout better).
What am I missing here?
You can use autolayout to set the position and size of the collection view. And you can use autolayout to set the position and size of the subviews inside each cell. But you cannot use autolayout to control the position and size of the cells. You must use the collection view's layout object to set the position and size of each collection view cell. If you want the cells to change size when the interface orientation changes, you must update your layout object to report the new size and invalidate the layout.

Constraints changed when modifying UIViewController.frame

I have a UIViewController which I add to another UIViewController as a child. When the child gets added, the parent set frame on my child using childVC.frame = ...., but this fubars my constraints.
I can duplicate this issue in IB easily. Create a UIViewController, change the size to be "Freeform" and ditch the status bar (just so it looks right visually). Add in a UIButton (or anything) and constraints so so they are both the same size (see image below)
Now change the size of the top level view in the view controller and watch what happens.
It changed my constraints on me. I wanted my button to just get bigger and still hold the original constraints. Something which happens automatically if you toggle between iPhone retina 3.5/4.0 in screen or change from landscape to portrait. But why not when I resize the view? This is making a royal pain to lay things out in freeform and test different sizes.
More importantly... why does the exact same thing happen when I set the frame in code when I add the child for my VC and how do I prevent it?
Frame based calculations and Auto Layout are not compatible. Or at least, they mean stormy waters. That's why you are having issues. Having switched to Auto Layout you have to accept that you no longer manipulate frame directly.
So, you cannot do childVC.frame = whatever.
Instead, you must now use Auto Layout to update the size of childVC.frame. Do that by creating - in IB - an IBOutlet that is the width constraint of childVC. Similarly, create another IBOutlet that is the childVC height constraint.
In your view controller, update the constant value to change the size.
myWidthConstraint.constant = 123.f;
The place to do this is in the controller's updateViewConstraints method, or in the case of a view, updateConstraints.
That said, it's better to set up Auto Layout so that you don't need any programmatic updates. When properly defined, it will know how to handle things with the view's bounds change. To be honest, it should only be necessary to update sizes programmatically if you want to do something more unusual.
Can I just say however that you are better

Resources