Stop autolayout from resizing my view - ios

Using Storyboards I add a UIView with height of 5px.
At run time the height of that view is increased (could be up to 25px).
When the orientation changes, the height of the view is reverted back to the original 5px.
How can I prevent this? I want the height to remain at whatever it was prior to the orientation change.
Sure I could detect orientation change and then change it back but that looks sloppy because it shrinks then increases right away.
Edit: This may or may not be an autolayout issue. Or might just be the default behavior of storyboards.

Add a constraint fixing the height, you may also need to remove a constraint. If you show the code or explain how you're setting the constants in the first place can give a more detailed answer.
Other answer is to remove the constraint that stretching the size of your view, may be that you have conflicting constraints.
One more answer could be you need to adjust the priority of an other constraint perhaps one for spacing between views. Again more detail = better answer.

Overriding -layoutSubviews is the incorrect approach. If you're using autolayout you should adjust a view's height by modifying its constraints programmatically.
Make a property for your view's height constraint:
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
I put IBOutlet there since you said you're making the constraints in a storyboard. Just drag the height constraint to that property to connect it. (You could also create the constraint programmatically, in which case you wouldn't use IBOutlet.)
Then, whenever you want to change the height:
self.heightConstraint.constant = 25;

The solution to the problem is to sublcass UIView and override -(void)layoutSubviews
In -(void)layoutSubviews is where I set my desired height

Related

Move UILabel using Autolayout constrains in XIB only?

Possible a duplicate, but I tried could not make it work so came here.
I (new to autolayout) have two UILabel place one below each other with fixed height space.Both can increase with as per text with in it.
When First UILabel hides bottom should move to First place. How to do it using constraints in view only? I know how to do by creating IBOutlet connection of constraints for second UILabel.
EDIT:
Given question is about more about content hugging related, where as my question is add constrains to move to first UILabel position when first hides.
The only way to achieve what you want with AutoLayout is to set constant of all related constraints to zero. And remember NEVER try to set frame or bounds of your view if you are using AutoLayout(unless you override layoutSubviews and do the stuff in that method, which you rarely need to).
You can check out this tiny project: https://github.com/neevek/UIView-Visibility, I bet that is what you want :-)

Automatically refactor objects from Autolayout

I read that you should not normally use setFrame when you are using autolayout.
However, if I have a constraint from a UIButton to a UITextView, lets say the UITextView's height changes and in the IB I set the UIButton's constraint to be 10 vertical unit spacings apart from the UITextView. When I change the UITextView's height by using setFrame, is there anyway to get the UIButton to automatically recalculate it's y position based on the constraint?
Thanks!
You really shouldn't modify the view's frame when using autolayout. You need to modifiy the constraint, wether by resetting it programmatically or by adding an IBOutlet that references the constraint and modify its constant value.
You might want to take a look at this article

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.

Change height constraint programmatically

I want to change the height constraint of a UITextView programmatically so I set the constraint as an outlet in my controller like this:
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *descriptionHeightConstraint;
To change it I do the following:
self.descriptionHeightConstraint.constant = self.description.contentSize.height;
This works if I do it in viewDidAppear but the problem with this is that I can see how the height changes after the view displayed which is not very user friendly so I tried doing it in viewWillAppear but didn't work there, the height doesn't change. Calling setNeedsUpdateConstraints after changing the constraint didn't work either.
Why is working in viewDidAppear and not in viewWillAppear? Any workaround?
Thanks in advance!
Try setting the constant in viewDidLayoutSubviews instead.
Note that using the text view's contentSize property to set the text view's height does not work in iOS 7. See https://stackoverflow.com/a/18837714/1239263

UIScrollView Not Scrolling?

I have a UIScrollView created in interface builder with a bunch of UITextView and UIImageView objects already added. I have it connected to an IBOutlet, and under -(void)viewDidLoad I set its content size to 1200x2000. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?
viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
Are you using autolayout? If so, check out the following answer (and forget 1.):
UIScrollView doesn't use autolayout constraints.
The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:
Leave autolayout enabled.
Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.
viewDidLoad is indeed too soon, but viewDidAppear:animated might not be late enough either. The ideal place to set the contentOffset is inside viewDidLayoutSubviews.
If you're using Autolayout, make sure you put the code to set Content size within
viewDidLayoutSubviews
I use a custom view on the UIScrollView, for pull to refresh function, and the scroll view is not scrollable. I try set contentSize with AutoLayout, and try set it by codes too. The scroll view still cannot scroll.
After spending 3 ~ 4 hours, I solve it by contentInset. It will tell scroll view to add some extra space for scrolling.
override func viewDidLayoutSubviews() {
// Add extra 15pt on the `top` of scrollView.
//self.scrollView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
// Add extra 2pt on the `bottom` of scrollView, let it be scrollable. And the UI is more beautiful in my case.
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 2, 0)
}
Reference docs:
Understanding the contentOffset and contentInset properties of the UIScrollView class #blog
What's the UIScrollView contentInset property for? #Stackoverflow
This is a good walk-through of scroll-views in case anyone forgot after not using them for a while: https://medium.com/#pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7.
Basically, make sure you have view -> scrollview -> view, like so:
then,
1. Set scroll view constraint (top, bottom, leading and trailing) to (0,0,0,0).
2. Set inner view constraint (top, bottom, leading and trailing) to (0,0,0,0).
3. Set inner view to have equal width and equal height with parent view.
4. Select height constraint of inner view and set priority to low (250).

Resources