How to change uinavigationbar title position?

I managed to change the height of the navigation bar using my own navigation bar, but the name is still focused. I want it to be in 72px position on the left.

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

I used this to change the height, but I did not find a way to change the position of all the elements. I tried to set the frame, but I can’t. I also can not change the position of the button.

I want to look like this

enter image description here

+4
source share
2 answers

UIView, UIButton UILabel , . .

:

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...
}
+11
navBar.setTitleVerticalPositionAdjustment(CGFloat(7), forBarMetrics: UIBarMetrics.Default)
+23

Source: https://habr.com/ru/post/1568083/


All Articles