ios - UIViewAutoresizingMask 将子视图宽度扩展到父视图之外
问题描述
我需要向一些人展示它在 Autolayout 之前的样子,所以我想尝试自动调整遮罩大小来构建一个如下所示的视图:
————————————
| ———————— |
| | red | |
| | | |
| ———————— |
| |
| yellow |
————————————
我希望红色 UIView 水平扩展,但在设备旋转时将其左侧、顶部和右侧边距保持在 20 点。
这是我的代码:
// .h
@interface NextViewController : UIViewController
@end
// .m
@interface NextViewController ()
@property (nonatomic) UIView *rectView;
@end
@implementation NextViewController
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectZero];
self.view.backgroundColor = [UIColor yellowColor];
// 280 width for iPhone 5S when left and right paddings are 20 points
self.rectView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 100)];
[self.view addSubview:self.rectView];
self.rectView.backgroundColor = [UIColor redColor];
self.rectView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
@end
我的问题是红色 UIView 总是超出黄色 UIView 的宽度:
我打印了红色视图的描述:
Printing description of $15:
<UIView: 0x7fc360e181c0; frame = (20 20; 600 100); autoresize = W; layer = <CALayer: 0x600000226960>>
那么我做错了什么?
解决方案
啊,我现在明白了。就是这一行:
self.view = [[UIView alloc] initWithFrame:CGRectZero];
子视图的自动调整大小掩码正在CGRectZero
根据顶层视图的这个框架进行评估,这就是它们没有正确调整大小的原因。我在纵向模式下给了它一个 iPhone 5S 屏幕的框架,并修复了旋转缩放:
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
推荐阅读
- javascript - Atom wrong autocomplete
- javascript - 设置为另一个对象的 Javascript 对象未定义
- noise-reduction - 谷歌云语音背景噪音
- java - If Statement not working with double
- javascript - Display html blank code in HTML page
- arrays - 当我在发布后从响应中推送一些数据时,Angular 4 不会更新数组
- javascript - Path 与 Route 匹配,但该路由的组件未呈现
- regex - 如何使用 REGEX 格式化“电话号码”?
- javascript - 节点 js require()[env] 未定义
- java - ModelAndView not returning model to the JSP in spring mvc