【问题标题】:UIView autoresizingMask bug when setting different fixed left and right margins and flexible width?UIView autoresizingMask 设置不同的固定左右边距和灵活宽度时的错误?
【发布时间】:2013-12-30 17:29:26
【问题描述】:

我正在尝试使用该代码创建一个带有分隔符的视图(如 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));

如您所见,我明确地将容器的宽度设置为 500 像素。但是,后来它更改为 258px。我得到的结果是:

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

【问题讨论】:

    标签: ios objective-c uiview autoresizingmask


    【解决方案1】:

    试一试

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

    【讨论】:

    • 我会称之为 UIView 中的错误
    • UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(13.0f, floorf(container.height / 2), container.width - 13.0f, 1.0f)]; 替换为UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(13.0f, floorf(container.frame.size.height / 2), container.frame.size.width - 13.0f, 1.0f)];
    • width 和 UIView 中的 height 是我的扩展,相应地返回 frame.size.width 和 frame.size.height
    猜你喜欢
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多