我正在尝试使用该代码创建一个带有分隔符的视图(如iOS 7中的表视图单元格):
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));
如您所见,我明确将容器宽度设置为500px。但是,稍后它会改为258px。而我得到的结果是:
如何摆脱分隔符的冗余右侧部分?我的autoresizingMask似乎并且当容器的宽度为500px时看起来正确:
答案 0 :(得分:1)
试一试
separator.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;