简体   繁体   English

需要UIView自动调整大小

[英]Need UIView to autoresize

I have made a custom UIView which is shown when the user hits a button in the navigationbar. 我制作了一个自定义UIView,当用户单击导航栏中的按钮时将显示该UIView。 I make my view's in code. 我使我的观点在代码中。 In my loadview I set the autoresizing masks and the view loads correct on screen. 在我的loadview中,我设置了自动调整大小的蒙版,并且视图在屏幕上正确加载。 However the UIView which is shown when the user taps the button does not resize even when I have set the autoresizing masks. 但是,即使我设置了自动调整大小蒙版,当用户点击按钮时显示的UIView也不会调整大小。

UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 416.0)];
blackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Do I need to use s elf.view.frame.size.width and self.view.frame.size.height instead? 我是否需要使用s elf.view.frame.size.widthself.view.frame.size.height呢? And if I do why? 如果我为什么呢? Does not resizing masks work outside of loadView? 调整遮罩大小是否在loadView之外不起作用?

Thank you for your time:) 感谢您的时间:)

the autoresizingMask affects how a view will behave when its superview s frame changes. if all you are doing is showing the autoresizingMask影响视图在其autoresizingMask视图s frame changes. if all you are doing is showing the时的行为s frame changes. if all you are doing is showing the s frame changes. if all you are doing is showing the blackView when you tap a button, then blackView` will have whatever frame you initially set for it. s frame changes. if all you are doing is showing the只是when you tap a button, then s frame changes. if all you are doing is showing the blackView when you tap a button, then blackView将具有您最初为其设置的任何框架。

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. 如果这还不够,请发布更多有关如何配置和显示blackView及其blackView视图的代码,并详细说明您期望blackView调整大小的情况。旋转就是其中之一,如果您blackView的话关注于。

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 假设需要调整大小的视图是:view2
The view that has view2 as a subview is: view1 将view2作为子视图的视图为:view1

While creating view1 you would declare it as: 创建view1时,您将其声明为:

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

Now in view1's .m file you need to overload the layoutSubviews method as shown: 现在,在view1的.m文件中,您需要重载layoutSubviews方法,如下所示:

- (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 如果view1是视图控制器的视图,则需要在willRotate方法中执行与上述相同的操作,如下所示

- (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. 这是我用来处理方向更改的一种久经考验的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM