How to change the uinavigationbar title's position?

前端 未结 4 420
广开言路
广开言路 2021-01-03 03:30

I\'ve managed to change the navigationbar height by using my own navigationbar, but the title is still centered. I want it to be at the 72px position from the left.

4条回答
  •  抹茶落季
    2021-01-03 04:17

    Create a UIView object add UIButton and UILabel in it to show a similar view. Add this custom view into left bar button item of navigation controller.

    An example screen shot to visualise a custom view and related code:

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

提交回复
热议问题