UIBarButtonItem 的垂直对齐方式在横向模式下太低

发布于 2024-11-26 03:01:40 字数 1464 浏览 0 评论 0原文

我使用以下代码向 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:
In Portrait

Landscape:
In Landscape

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2024-12-03 03:01:40

UIViewAutoresizingFlexibleHeight 添加到您的 titleView。你不会让它缩小标签。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文