Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

objective c - Center UIButton programmatically

I have a UIButton which i add programmatically. No problems here, it stetches as it should and looks good. However, when I tilt the phone to landscape the button gets out of place, and I want it still centered.

Portrait: It's centered here

Landscape: Not centered here

I have tried a bunch of thing but cannot seem to get it working, it seems to me that the autoresizing should take care of it, however... it doesn't work in that case.

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 49)];

    _notMeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *butImage = [[UIImage imageNamed:@"notme.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6];
    UIImage *butImagePressed = [[UIImage imageNamed:@"KC_notmePressed.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6];
    [_notMeButton setBackgroundImage:butImage forState:UIControlStateNormal];
    [_notMeButton setBackgroundImage:butImagePressed forState:UIControlStateHighlighted];
    [_notMeButton setTitle:NSLocalizedString(@"Change address", @"KlarnaCheckout") forState:UIControlStateNormal];
    [_notMeButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [_notMeButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [_notMeButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_notMeButton.titleLabel setShadowColor:[UIColor whiteColor]];
    [_notMeButton.titleLabel setShadowOffset:CGSizeMake(0, 1)];
    [_notMeButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [_notMeButton addTarget:self action:@selector(thisIsNotMe) forControlEvents:UIControlEventTouchUpInside];
    [_notMeButton setTag:1];
    [_notMeButton sizeToFit];
    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
    [_notMeButton setFrame:CGRectMake(10, 10, _notMeButton.frame.size.width+20, _notMeButton.frame.size.height)];
    [_notMeButton setCenter:footerView.center];

    [footerView addSubview:_notMeButton];

    return footerView;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your idea about autoresizing mask is right, but you're setting it incorrectly:

[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

The second line overwrites what you set in the 1st. Correct way to set several flags will be:

[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...