我正在尝试使用该代码创建带有左插入的分隔符的视图(如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。结果是:



我应该怎么做才能摆脱分隔器多余的右侧部分?当容器的宽度为500px时,我的autoresizingMask看起来和看起来都正确:

最佳答案

试试

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

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

10-09 16:33