0

iOS navigation bar custom titleView subviews are not showing properly when the parent viewController pushed from another view controller.

Custom Title view in root view enter image description here

Custom Title view in View controller after pushing from the different view controller

enter image description here

Code I tried for Custom TitleView

   let height = CGFloat(500)
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 50, width: view.frame.width, height: height)
    self.navigationController?.navigationBar.backgroundColor = .blue
    let navView = UIImageView()
    navView.frame = CGRect(x: 0, y: 0, width: view.frame.width-100, height: height)
    navView.backgroundColor = .red
    navigationItem.titleView = navView

Suview

        let oneLabel = UILabel()
        oneLabel.frame = CGRect(x: 50, y: navView.frame.size.height-13, width: 26, height: 26)
        oneLabel.text = "1"
        oneLabel.textColor = .white
        oneLabel.backgroundColor = UIColor(hexString: Constants.greenColor)
        oneLabel.textAlignment = .center
        navView.addSubview(oneLabel)

        oneLabel.clipsToBounds = true
        oneLabel.layer.cornerRadius = 13
2
  • why your navigation bar heigh is 500? Commented Mar 13, 2020 at 8:27
  • @Code cracker here: oneLabel.frame = CGRect(x: 50, y: navView.frame.size.height-13, width: 26, height: 26), Why you subtract navView.frame.size.height-13 in Y position? I think you just set like 44 / 2 = 22 on there. Commented Mar 13, 2020 at 9:16

1 Answer 1

0

The total height of the label is 26 and u have reduced only 13 from the imageview's height which shows half of the label.. Make your y value of label something like y: navView.frame.size.height - 30

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.