stackich
stackich

Reputation: 5287

Can I make large rightBarButtonItem in Swift?

What I have:
enter image description here

self.title = "Title"
navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItem?.tintColor = .white

Is it possible to make this Add looks exactly like this Title?
Something like this:
enter image description here

Or any other solution for this - title left - button with image right, same heights?

Upvotes: 0

Views: 1207

Answers (2)

Md Hanif
Md Hanif

Reputation: 1107

please go through this answer which shows how you can manage Navigation bar title when collapsed and when it's large. It won't give you an exact answer but it will definitely help you in achieving what you want.

Other then that please go through this answer which will help you understand how give x,y positions to right bar button item.

Just a quick overview how you can achieve what you want by the combination of this two answers:-

  1. Set the position of the right bar button item according to the position or height of the navigation bar which you wanted to do. Link number 2 will help you in doing that.
  2. Observe your navigation bar when the height of your navigation bar is increased or decreased you can change the position of your right bar button item.

and Done. By using this you can manage the position of your bar button item at both the item when your Navigation bar is Enlarged at that time you can show button at different position and when the Navigation Bar is collapsed you can show button at different position so that it doesn't look vice-versa of your question after changing position of your button.

Upvotes: 1

Steve B
Steve B

Reputation: 600

I've done something similar in my app - not exactly what you are looking for, but should give you enough to go on:

    func setupNavBar() {
    let rightButton = UIButton()
    rightButton.setTitle("Leave", for: .normal)
    rightButton.addTarget(self, action: #selector(rightButtonTapped(button:)), for: .touchUpInside)
    navigationController?.navigationBar.addSubview(rightButton)
    rightButton.tag = 97
    rightButton.frame = CGRect(x: self.view.frame.width, y: 0, width: 120, height: 20)

    let targetView = self.navigationController?.navigationBar
    let trailingConstraint = NSLayoutConstraint(item: rightButton, attribute:
        .trailingMargin, relatedBy: .equal, toItem: targetView,
                         attribute: .trailingMargin, multiplier: 1.0, constant: -16)
    let bottomConstraint = NSLayoutConstraint(item: rightButton, attribute: .bottom, relatedBy: .equal,
                                              toItem: targetView, attribute: .bottom, multiplier: 1.0, constant: -6)
    rightButton.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([trailingConstraint, bottomConstraint])
    }

Custom Right Button

I also created this function to remove it (hence the tag use above):

    func removeRightButton(){
        guard let subviews = self.navigationController?.navigationBar.subviews else{
            log.info("Attempt to remove right button but subviews don't exist")
            return
        }
        for view in subviews{
            if view.tag == 97 {
                view.removeFromSuperview()
            }
        }
    }
    

Upvotes: 0

Related Questions