ios - UIView autoresizingMask 在设置不同的固定左右边距和灵活宽度时出现错误?

标签 ios objective-c uiview autoresizingmask

我正在尝试使用该代码创建一个带有左插入分隔符的 View (如 iOS 7 中的表格 View 单元格):

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 500.0f, 90.0f)];
container.backgroundColor = [UIColor whiteColor];
container.autoresizingMask = UIViewAutoresizingFlexibleWidth;
container.layer.cornerRadius = 5.0f;
container.layer.borderColor = grayColor.CGColor;
container.layer.borderWidth = 1.0f;
NSLog(@"container: %@", NSStringFromCGRect(container.bounds));

UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(13.0f, floorf(container.height / 2), container.width - 13.0f, 1.0f)];
separator.backgroundColor = grayColor;
separator.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[container addSubview:separator];
NSLog(@"separator: %@", NSStringFromCGRect(separator.frame));

如您所见,我将容器的宽度明确设置为 500 像素。然而,后来它变成了 258px。结果我得到的是:

enter image description here

我应该怎么做才能去掉分隔符右边多余的部分?当容器的宽度为 500px 时,我的 autoresizingMask 看起来是正确的:

enter image description here

最佳答案

尝试一下

separator.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleTopMargin UIViewAutoresizingFlexibleBottomMargin;

关于ios - UIView autoresizingMask 在设置不同的固定左右边距和灵活宽度时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20836628/

相关文章:

ios - 在 UICollectionViewCell 中添加观察者。获取异常 KVO_IS_RETAINING_ALL_OBSERVERS_OF_THIS_OBJECT_IF_IT_CRASHES_AN_OBSERVER_WAS_OVERRELEASED

ios - HTTPBody - Swift POST 请求 - 推特

objective-c - 使用 KVO 更新由 NSPredicate 过滤的 NSTableView

swift - 如何在 Swift 中以编程方式 move UIView

ios - 无法重现应用程序卡住,如何调试?

objective-c - UILabel 的 backgroundColor 是不可动画的吗?

objective-c - 覆盖 "release"或 "dealloc"

ios - 何时在 Swift 中子类化 NSObject

ios - UIView 编程约束不起作用

objective-c - 在 iOS8 中附加到 UIWindow 时保持 UIView 固定