如何改变uinavigationbar冠军的位置?

时间:2014-12-18 04:05:23

标签: swift ios8 uinavigationbar material-design

我设法通过使用自己的导航栏来更改导航栏高度,但标题仍然居中。我希望它位于左起72px的位置。

override func sizeThatFits(size: CGSize) -> CGSize {
   return CGSizeMake(UIScreen.mainScreen().bounds.width, 56)
}

我用它来改变高度,但我找不到改变所有物品位置的方法。 我试着设置框架,但我不能。我也无法改变按钮的位置。

我想看起来像这样

enter image description here

5 个答案:

答案 0 :(得分:25)

navBar.setTitleVerticalPositionAdjustment(CGFloat(7), forBarMetrics: UIBarMetrics.Default)

答案 1 :(得分:18)

在其中创建UIView对象添加UIButtonUILabel以显示类似的视图。将此自定义视图添加到导航控制器的左栏按钮项中。

可视化自定义视图和相关代码的示例屏幕截图:

enter image description here

override func viewDidLoad() {
    super.viewDidLoad()

    var customView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 44.0))
    customView.backgroundColor = UIColor.yellow

    var button = UIButton.init(type: .custom)
    button.setBackgroundImage(UIImage(named: "hamburger"), for: .normal)
    button.frame = CGRect(x: 0.0, y: 5.0, width: 32.0, height: 32.0)
    button.addTarget(self, action: #selector(menuOpen(button:)), for: .touchUpInside)
    customView.addSubview(button)

    var marginX = CGFloat(button.frame.origin.x + button.frame.size.width + 5)
    var label = UILabel(frame: CGRect(x: marginX, y: 0.0, width: 65.0, height: 44.0))
    label.text = "Inbox"
    label.textColor = UIColor.white
    label.textAlignment = NSTextAlignment.right
    label.backgroundColor = UIColor.red
    customView.addSubview(label)

    var leftButton = UIBarButtonItem(customView: customView)
    self.navigationItem.leftBarButtonItem = leftButton
}

func menuOpen(button: UIButton) {
    // Handle menu button event here...
}

答案 2 :(得分:0)

另一种解决方案是简单地缩进标题:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 72

navigationBar.titleTextAttributes = [
    .foregroundColor: UIColor.purple,
    .paragraphStyle: paragraphStyle
]

答案 3 :(得分:0)

使用左对齐设置自定义视图。

将自定义视图设置为leftBarButtonItem并不方便,因为后退按钮会出现问题。

将自定义视图设置为navigationBar的子视图并不方便,因为:

  • 设置右键后,我们无法轻松控制自定义视图的宽度。我们可以获得重叠的自定义视图和右键按钮
  • 我们需要添加自定义的navTitleView并在每个viewWillAppear和viewWillDissapear处将其删除。这是附加的,不是干净的代码。

是吗? 我刚刚在customTitleView中添加了宽度限制。

在CustomTitleView类内部:

  private func layoutViewsConfig() {
    // Width constraint needed to align view to left
    let widthConstraint = NSLayoutConstraint.init(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: UIScreen.main.bounds.width)
    widthConstraint.priority = .init(748)
    let heightConstraint = NSLayoutConstraint.init(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
   self.addConstraint(widthConstraint)
   self.addConstraint(heightConstraint)    
  }        

然后,在您的ViewController中:

var navigationTitleView = NavigationSubtitleView() 
self.navigationItem.titleView = navigationTitleView

我们有:

enter image description here

答案 4 :(得分:0)

您可以将自定义视图设置为导航项的左/右栏项。

看这个例子:

<img>

[navigationItem.rightBarButtonItem = UIBarButtonItem(customView: viewModal.getNavBarTitleView()) 返回一个带有 imageView 和标签的水平堆栈视图。]