如何更改uinavigationbar标题的位置?

Dav*_*vid 5 uinavigationbar swift ios8 material-design

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

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

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

我想看起来像这样

在此输入图像描述

Arb*_*eli 28

navBar.setTitleVerticalPositionAdjustment(CGFloat(7), forBarMetrics: UIBarMetrics.Default)
Run Code Online (Sandbox Code Playgroud)

  • 此答案仅解决项目的垂直位置.这是如何回答有关调整水平位置的问题? (6认同)
  • @TonyAdams我想我当时不知道问题的答案,所以我只是在空中扔东西,可以帮助别人...... (4认同)
  • 凉爽的!这就是我需要的!! (2认同)

Kam*_*pai 19

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

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

在此输入图像描述

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...
}
Run Code Online (Sandbox Code Playgroud)