UIBarButtonItem 的垂直对齐方式在横向模式下太低
我使用以下代码向 UIToolbar (iPad) 添加标题:
- (void)setupToolbarItems {
if (!toolbarItems) {
UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 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];
}
纵向模式下一切正常,但在横向模式下标签太靠下。 UIBarButtonItem 不是视图,因此据我所知,我无法从代码中调整其位置......我该如何解决这个问题?
肖像:
风景:
I'm adding a title to a UIToolbar (iPad) with the following code:
- (void)setupToolbarItems {
if (!toolbarItems) {
UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
UIViewAutoresizingFlexibleHeight
添加到您的 titleView。你不会让它缩小标签。Add
UIViewAutoresizingFlexibleHeight
to your titleView. You're not letting it shrink the label.