开发者

Need UIView to autoresize

开发者 https://www.devze.com 2023-02-26 04:03 出处:网络
I have made a custom UIView which is shown when the user hits a button in the navigationbar. I make my view\'s in code. In my loadview I set the autoresizing masks and the view loads correct on screen

I have made a custom UIView which is shown when the user hits a button in the navigationbar. I make my view's in code. In my loadview I set the autoresizing masks and the view loads correct on screen. However the UIView which is shown when the user taps the button does not resize even when I have set the autoresizing masks.

UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 416.0)];
blackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexib开发者_开发技巧leHeight;

Do I need to use self.view.frame.size.width and self.view.frame.size.height instead? And if I do why? Does not resizing masks work outside of loadView?

Thank you for your time:)


the autoresizingMask affects how a view will behave when its superviews frame changes. if all you are doing is showing theblackViewwhen you tap a button, thenblackView` will have whatever frame you initially set for it.

If this isn't enough info, please post some more code around how you are configuring and displaying blackView and it's superview and explain more about what situations you are expecting blackView to resize in. Rotation is one of them, if that's what you're concerned with.


First things first, I hope you've done this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

Let's say the view that needs resizing is: view2
The view that has view2 as a subview is: view1

While creating view1 you would declare it as:

view1 = [[UIView alloc] init];
[view1 setNeedsLayout];

Now in view1's .m file you need to overload the layoutSubviews method as shown:

- (void)layoutSubviews
{
    CGRect frame = view2.frame;
    // apply changes to frame
    view2.frame = frame;
}

In case view1 is a view controller's view, you need to do that same thing as above in the willRotate method as shown

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    CGRect frame = view2.frame;
    // apply changes to frame
    view2.frame = frame;
}

This is a tried and tested method that I use to handle orientation changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消