tendai
tendai

Reputation: 1250

Failing to perform click on navigation bar custom button

I have a ViewController with a navigation bar in which i have 3 custom buttons that i have programatically assigned click events. The navigation bar looks like this:

enter image description here

The issue i'm facing somehow is that the first 2 buttons ('Home' & 'Events') can click well and respond accordingly, but the third ('Today') won't click. I am puzzled as to why and thought maybe this is the spot where normally the Navigation Bar title is so maybe there is some conflict but i can't seem to resolve it

The method adding these buttons and click events is as follows:

func addCustomBtnsNavBar(){
    self.title = nil
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()

    let viewLeftItem = UIView(frame: CGRect.init(x: 0, y: 5, width: 120, height: 40))

    let button1a = UIButton(frame: CGRect.init(x: 0, y: 5, width: 50, height: 40))
    button1a.setTitle("Home", for: .normal)
    button1a.titleLabel?.font =  UIFont(name: "Nolasco Sans", size: 16)
    button1a.setTitleColor(.black, for: .normal)

    button1a.addTarget(self, action: #selector(self.openAboutUs), for: .touchUpInside)

    let button1b = UIButton(frame: CGRect.init(x: 60, y: 5, width: 50, height: 40))
    button1b.setTitle("Events", for: .normal)
    button1b.titleLabel?.font =  UIFont(name: "Nolasco Sans", size: 16)
    button1b.setTitleColor(Util.hexStringToUIColor(hex: Constants.lightGrey), for: .normal)
    button1b.addTarget(self, action: #selector(self.openEventsPage), for: .touchUpInside)

    let button1c = UIButton(frame: CGRect.init(x: 120, y: 5, width: 50, height: 40))
    button1c.setTitle("Today", for: .normal)
    button1c.titleLabel?.font =  UIFont(name: "Nolasco Sans", size: 16)
    button1c.setTitleColor(Util.hexStringToUIColor(hex: Constants.lightGrey), for: .normal)
    button1c.addTarget(self, action: #selector(self.openEventsPage), for: .touchUpInside)
    let leftBarButton = UIBarButtonItem(customView: viewLeftItem)

    let viewRightItem = UIView(frame: CGRect.init(x: 0, y: 0, width: 60, height: 30))

    let button2 = UIButton(frame: CGRect.init(x: 35, y: 0, width: 30, height: 30))
    button2.setBackgroundImage(UIImage(named: "blue_circle"), for: .normal)
    button2.setTitle(getInitials(), for: .normal)
    button2.titleLabel?.font =  UIFont(name: "Google Sans", size: 12)
    button2.setTitleColor(.white, for: .focused)
    button2.addTarget(self, action: #selector(self.didTapRightButton), for: .touchUpInside)

    viewLeftItem.addSubview(button1a)
    viewLeftItem.addSubview(button1b)
    viewLeftItem.addSubview(button1c)

    viewRightItem.addSubview(button2)

    let rightBarButton = UIBarButtonItem(customView: viewRightItem)

    self.navigationItem.leftBarButtonItem = leftBarButton
    self.navigationItem.rightBarButtonItem = rightBarButton
}

Upvotes: 0

Views: 405

Answers (1)

Savca Marin
Savca Marin

Reputation: 437

viewLeftItem's width is 120, and the x origin of your third button is 120.

I do believe that if you'll set viewLeftItem's clipToBounds property to true, you won't be able to see it at all.

You have to increase the width of your container to 170 according to the origin and the width of your third button.

Upvotes: 2

Related Questions