iOS Autolayout - Get Frame Size Width

iOS AutoLayout - get frame size width

I think auto layout hasn't had the time to layout your views by the time you call this. Auto layout hasn't happened by the time viewDidLoad is called, because it's called just after the views are loaded and it's only after that that the views are placed into the view controller's view hierarchy and eventually laid out (in the view's layoutSubviews method).

Edit: this answer points out why the scenario in the question doesn't work. @dreamzor's answer points out where to place your code in order to solve it.

Get frame of a view after autolayout

Your underline view has a static frame, it is not connected to the textField through constraints. Instead of setting the frame, add constraints to self.inputView

- (void)underlineTextField:(UITextField *)tf {
UIView *line = [[UIView alloc] init];
line.backgroundColor = [UIColor whiteColor];
[line setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.inputView addSubview:line];

// Vertical constraints
[self.inputView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[line(==1)]-(-1)-|" options:0 metrics:nil views:@{ @"line": line}]];

// Horizontal constraints
[self.inputView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(-8)-[line]-8-|" options:0 metrics:nil views:@{ @"line": line}]];

[self.inputView layoutIfNeeded];
}

After the layoutIfNeeded call, the frame for your view should be right. I hoped I got the constants right. Because the line appears one unit under the textView make sure to unset Clip Subviews in the storyboard for the textField

I hope this works for you. Let me know if you have questions!

How to get UIImageView size after autolayout is applied?

In order to get the right frame/bounds of your UIImageView after resizing, you need first ask auto-layout to update that layout using [yourImageView layoutIfNeeded]. that will solve your constraints and update your yourImage.bounds.

[myImageView layoutIfNeeded];
NSLog(@"w: %f, h: %f", myImageView.bounds.size.width, myImageView.bounds.size.height);

How to get real size UIView with autolayout

The simplest solution is to just run that code in -viewDidAppear: (after autolayout has been run).

When is view frame size set in iOS10 SDK?

In iOS 10, I met this issue right after I started to use Xcode 8 in june, when it was beta. This solution is needed:

Put layoutIfNeeded() right before your layer modifications:

self.view.layoutIfNeeded()

view.layer.cornerRadius = view.frame.size.width / 2
view.clipsToBounds = true

OR

place the code for corner radius to viewDidLayoutSubviews() method:

override func viewDidLayoutSubviews() {

view.layer.cornerRadius = view.frame.size.width / 2
view.clipsToBounds = true

}

How to get frame from UIView subclass with AutoLayout

After battling around with it I've figured out what was happening.

layoutSubviews() is indeed the way to go;
It will calculate the frames of the view and it's direct subviews.

The problem here is that the UILabels are not subviews of the main UIView subclass, but rather are subviews of the main UIView's subview (effect view).

Your view hierarchy example:

--> TestView
--> EffectView
--> UILabel
--> UILabel
--> UILabel

As you can see, layoutSubviews() will give you the correct frames for TestView and EffectView since it's its direct subview, but won't give you the calculated frames from UILabels because they aren't direct subviews of TestView.

Why is frame width of the view alway 600 by 600 with autolayout

When you are using auto-layout, the subviews are laid out after the viewDidLayoutSubviews function. Therefore, if you call tileContainer.frame.size before that, such as in viewDidLoad, it will always be 600 by 600 (which is the default size in storyboard).

viewDidLayoutSubviews: Called to notify the view controller that its view has just laid out its subviews. reference

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

print(tileContainer.frame.size); // This is actual size you are looking for
}


Related Topics



Leave a reply



Submit