开发者

Vertical alignment of UIBarButtonItem is too low in landscape mode

开发者 https://www.devze.com 2023-03-22 20:30 出处:网络
I\'m adding a title to a UIToolbar (iPad) with the following code: - (void)setupToolbarItems { if (!toolbarItems) {

I'm adding a title to a UIToolbar (iPad) with the following code:

- (void)setupToolbarItems {
if (!toolbarItems) {
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSp开发者_运维问答ace target:nil action:nil];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 386.0, 20.0)];
    titleLabel.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth);
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"Hello";
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0];
    titleLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    titleLabel.shadowOffset = CGSizeMake(0.0, -1.0);
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    UIBarButtonItem *titleLabelItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
    toolbarItems = [[NSArray alloc] initWithObjects:flexSpacer, titleLabelItem, flexSpacer, nil];
    [titleLabel release];
    [flexSpacer release];
    [titleLabelItem release];
}

[self setItems:self.toolbarItems animated:NO];
}

Everything works fine in portrait, but in landscape mode the label is too far down. UIBarButtonItem isn't a view, so I have no way of adjusting its position from my code as far as I can see…how can I fix this?


Portrait:

Vertical alignment of UIBarButtonItem is too low in landscape mode

Landscape:

Vertical alignment of UIBarButtonItem is too low in landscape mode


Add UIViewAutoresizingFlexibleHeight to your titleView. You're not letting it shrink the label.

0

精彩评论

暂无评论...
验证码 换一张
取 消