HiveHicks
HiveHicks

Reputation: 2334

UIView autoresizingMask bug when setting different fixed left and right margins and flexible width?

I'm trying to create a view with a separator that has left inset (like table view cells in iOS 7) using that code:

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));

As you can see, I explicitly set width of container to 500px. However, later it changes to 258px. And what I get as a result is that:

enter image description here

What should I do to get rid of the redundant right part of the separator? My autoresizingMask seems and looks right when the width of container if 500px:

enter image description here

Upvotes: 0

Views: 1595

Answers (1)

KudoCC
KudoCC

Reputation: 6952

Have a try

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

Upvotes: 1

Related Questions