我可以将 UIToolbar 项目居中吗?

发布于 2024-08-26 06:33:04 字数 284 浏览 5 评论 0原文

我正在 UIToolbar 上放置一个标签(根据此提示:将 UILabel 添加到 UIToolbar< /a>)。

但工具栏的左侧有一个按钮,因此灵活的空间使标签偏离了中心,而这正是我希望的位置。我能否以某种方式使该工具栏项目居中,使其在工具栏中保持居中?

(已经尝试用虚拟固定空间平衡按钮,没有骰子。)

谢谢!

I am putting a label on a UIToolbar (per this tip: Adding a UILabel to a UIToolbar).

But the toolbar has a button on the left side, and so flexible spaces throw the label off the center, which is where I'd like it to be. Can I somehow center this toolbar item so that it remains centered in the toolbar?

(Already tried balancing the button with a dummy fixed space, no dice.)

Thanks!

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

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

发布评论

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

评论(5

意中人 2024-09-02 06:33:04

像您一样,在中心项目的左侧和右侧添加一个灵活的空间项目。然后在末尾添加一个固定的空间项目,并将宽度设置为左侧按钮的宽度——大约 28 像素。

UIBarButtonItem *fixedItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL] autorelease];
fixedItem.width = 28;

Add a flexible space item to the left and right of your center item, like you did. Then add a fixed space item at the end and set the width to whatever the left button width is -- somewhere around 28 pixels.

UIBarButtonItem *fixedItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL] autorelease];
fixedItem.width = 28;
骑趴 2024-09-02 06:33:04

要将文本/标题放在工具栏上居中,您可以使用简单的 UILabel ,如下所示:

UILabel *labelTitle = [[UILabel alloc] init];
labelTitle.font =[UIFont fontWithName:@"Helvetica-Bold" size:18];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textAlignment = UITextAlignmentCenter;
labelTitle.userInteractionEnabled = NO;
labelTitle.text = @"Your Title";
[labelTitle sizeToFit];
CGRect labelTitleFrame = labelTitle.frame;
labelTitleFrame.size.width = self.toolbar.bounds.size.width;
labelTitleFrame.origin.y = (self.toolbar.bounds.size.height - labelTitleFrame.size.height) / 2;
labelTitle.frame = labelTitleFrame;
labelTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Just add UILabel like UIToolBar's subview 
[self.toolbar addSubview:labelTitle];
[labelTitle release];
labelTitle = nil;

To center a text/title on a toolbar you can use a simple UILabel like see below:

UILabel *labelTitle = [[UILabel alloc] init];
labelTitle.font =[UIFont fontWithName:@"Helvetica-Bold" size:18];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textAlignment = UITextAlignmentCenter;
labelTitle.userInteractionEnabled = NO;
labelTitle.text = @"Your Title";
[labelTitle sizeToFit];
CGRect labelTitleFrame = labelTitle.frame;
labelTitleFrame.size.width = self.toolbar.bounds.size.width;
labelTitleFrame.origin.y = (self.toolbar.bounds.size.height - labelTitleFrame.size.height) / 2;
labelTitle.frame = labelTitleFrame;
labelTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Just add UILabel like UIToolBar's subview 
[self.toolbar addSubview:labelTitle];
[labelTitle release];
labelTitle = nil;
放我走吧 2024-09-02 06:33:04

最简单的方法是将标签放在栏的上方,在其父视图中,并在那里对齐。

Simplest way would be to put the label above the bar, in it's parent view, and justify it there.

顾忌 2024-09-02 06:33:04

是的,我们可以将工具栏项目居中,只需在工具栏中放置两个灵活的空间,例如:

UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

toolbar.items = [NSArray arrayWithObjects:loginButton,flexibaleSpaceBarButton,toolBarLabel,flexibaleSpaceBarButton, nil];

Yes we can center a toolbar item just put two flexible space in the toolbar like :

UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

toolbar.items = [NSArray arrayWithObjects:loginButton,flexibaleSpaceBarButton,toolBarLabel,flexibaleSpaceBarButton, nil];
So要识趣 2024-09-02 06:33:04

如果您不使用灵活或固定空间,您可以修改内部 UIToolbar UIStackView 分布,如下所示。请注意,您应该在视图控制器的 viewDidLayoutSubviews() 方法中调用 toolbar.centerItemsHorizo​​ntally()

extension UIView {
    func allSubviews() -> [UIView] {
        var _allSubviews: [UIView] = []
        for subview in self.subviews {
            _allSubviews += subview.allSubviews()
            _allSubviews.append(subview)
        }
        return _allSubviews
    }
}
extension UIToolbar {
    func centerItemsHorizontally() {
        allSubviews().forEach { subview in
            if let stackView = subview as? UIStackView {
                stackView.distribution = .fillEqually
            }
        }
    }
}

结果:
输入图片此处描述

If you DON'T use flexible or fixed spaces, you can modify the internal UIToolbar UIStackView distribution as below. Note that you should call toolbar.centerItemsHorizontally() in your view controller's viewDidLayoutSubviews() method.

extension UIView {
    func allSubviews() -> [UIView] {
        var _allSubviews: [UIView] = []
        for subview in self.subviews {
            _allSubviews += subview.allSubviews()
            _allSubviews.append(subview)
        }
        return _allSubviews
    }
}
extension UIToolbar {
    func centerItemsHorizontally() {
        allSubviews().forEach { subview in
            if let stackView = subview as? UIStackView {
                stackView.distribution = .fillEqually
            }
        }
    }
}

Result:
enter image description here

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