方向更改后的UIView addsubview:告诉视图调整大小 [英] UIView addsubview after orientation change: Tell view to resize

查看:236
本文介绍了方向更改后的UIView addsubview:告诉视图调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在纵向模式下有一个UIView作为XIB.

I have a UIView as a XIB in Portrait mode.

此视图以编程方式添加到视图控制器中,如下所示:

This view is added programmatically to the viewcontroller like this:

NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"InputView" owner:self options:nil];
    InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
    [self.view addSubview:inputView];

此视图具有正确设置的自动调整大小蒙版,并且当方向从纵向"更改为横向"时可以旋转得很好.

This view has autoresizing masks set up properly and rotates fine when the orientation changes from Portrait to landscape.

但是,如果方向已经是 横向,并且我在方向更改后 创建了视图,则它具有其初始纵向方向.

However, if the orientation is already landscape and I create the view after the orientation change, it has its initial portrait orientation.

是否有一种方法可以告诉视图使用蒙版对其进行初始化或将其调整为纵向?

Is there a way to tell the view to initialize or resize itself to portrait by using its masks?

在此先感谢您的答复!

使用occulus和Inder Kumar Rathore的建议(谢谢大家!),我将代码更改为:

Using the suggestions of occulus and Inder Kumar Rathore (thanks guys!), I altered the code to this:

InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
[self.view setNeedsLayout];
[self.view layoutSubviews];
[self.view layoutIfNeeded];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[self shouldAutorotateToInterfaceOrientation:orientation];

不幸的是,根本没有任何改变. 我想我发现有人问同样的问题:

Unfortunately, there is no change at all. I think I found someone asking the same question:

答案正确地识别了问题,但不是很令人鼓舞... 当然,我可以创建两个笔尖或调整框架的大小,但这似乎与自动调整大小的想法背道而驰. 我发现很难相信,唤醒后无法告诉笔尖并将其添加到视图中以使用其自动调整大小功能...当设备旋转时,这种方法可以完美工作.

The answer identifies the problem correctly, but is not very encouraging... Sure, I could create two nibs or resize the frame, but this seems so contrary to the idea of auto-resizing. I find it hard to believe that there is no way to tell a nib after awakening and adding it to a view to use its autoresize features...something that works flawless when the device rotates.

idz的解决方案有效:

The solution of idz works:

InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
inputView.frame = self.view.bounds;
[inputView show];

谢谢!

推荐答案

NIB/XIB文件通常包含一个UIViewController来处理所有这些问题.在这种情况下,由于它们不是视图控制器(在NIB/XIB中),因此您需要接管其后加载任务.

Often a NIB/XIB file contains a UIViewController that takes care of all of this. In this case, since their is no view controller (in the NIB/XIB) you need to take over its post-load duties.

直接调用layoutSubviews或通过setNeedsLayoutlayoutIfNeeded间接调用layoutSubviews对您没有多大好处,因为默认实现不执行任何操作.

Calling layoutSubviews directly, or indirectly via setNeedsLayout or layoutIfNeeded won't do you much good because the default implementation does nothing.

假设您希望输入视图填充self.view的边界,请执行以下操作:

Assuming you want input view to fill the bounds of self.view you do the following:

InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
inputView.frame = self.view.bounds;
[inputView show];

必须正确设置所有子视图的调整尺寸蒙版 ,显然,如果您不想填充整个边界,则可能需要调整框架.

All the resize masks of the sub-views must be correctly set for this to work and, obviously, if you don't want to fill the full bounds you may want to adjust the frame.

这篇关于方向更改后的UIView addsubview:告诉视图调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆