【问题标题】:Can I center a UIToolbar item?我可以将 UIToolbar 项目居中吗?
【发布时间】:2011-01-31 10:53:16
【问题描述】:

我在 UIToolbar 上贴了一个标签(根据这个提示:Adding a UILabel to a UIToolbar)。

但是工具栏的左侧有一个按钮,因此灵活的空间使标签偏离了中心,这正是我想要的位置。我可以以某种方式将此工具栏项居中,使其在工具栏中保持居中吗?

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

谢谢!

【问题讨论】:

    标签: iphone uilabel uibarbuttonitem uitoolbar


    【解决方案1】:

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

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

    【讨论】:

    • 这假设系统字体永远不会改变(它有好几次)并且用户永远不会设置不同大小的字体。它还会忽略不同宽度的按钮标题的本地化。
    【解决方案2】:

    要在工具栏上居中文本/标题,您可以使用简单的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;
    

    【讨论】:

    • 当你改变方向和有左或右栏按钮时,这个方法可以正常工作
    【解决方案3】:

    最简单的方法是将标签放在栏的上方,在它的父视图中,并在那里证明它的合理性。

    【讨论】:

    • 感谢其他-Ben,那是我的 B 计划。:)
    • EricS 的方法更优越,更容易实现。
    • EricS 的方法是“静态的”,当你改变方向时不起作用
    【解决方案4】:

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

    UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    toolbar.items = [NSArray arrayWithObjects:loginButton,flexibaleSpaceBarButton,toolBarLabel,flexibaleSpaceBarButton, nil];
    

    【讨论】:

      【解决方案5】:

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

      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
                  }
              }
          }
      }
      

      结果:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 2013-08-10
        • 2018-04-07
        • 2011-12-21
        • 1970-01-01
        相关资源
        最近更新 更多