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:
Landscape:
Add UIViewAutoresizingFlexibleHeight
to your titleView. You're not letting it shrink the label.
精彩评论