Add custom button to navigation controller without border

前端 未结 3 806
醉酒成梦
醉酒成梦 2020-12-31 07:04

I add custom button to navigation controller

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@\"back.png\"]
                  


        
相关标签:
3条回答
  • 2020-12-31 07:18

    For Swift 4

    let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
    backButton.setImage(UIImage(named: "back.png"), for: .normal)
    backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: backButton)
    

    And action (selector) should be like the following :

    @objc func backAction () {
            // do the magic 
    }
    
    0 讨论(0)
  • 2020-12-31 07:26

    Try this.

    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, button_width, button_height)];
    [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    0 讨论(0)
  • 2020-12-31 07:27

    Adding Button either in left or right with title or without title as per the requirement,

    func addBarButton(image: UIImage?, isLeft: Bool = true, title: String? = nil, font: UIFont = UIFont.systemFont(ofSize: 16), tintColor: UIColor = R.color.textfieldColor() ?? .black) {
                if let navController = self.navigationController {
                    let navBar = navController.navigationBar
                    navBar.isHidden = false
                    navBar.isTranslucent = false
                    let btn = UIButton.init(type: .custom)
                    if let img = image {
                        btn.setImage(img, for: UIControl.State.normal)
                    }
                    if let titleTxt = title {
                        btn.setTitle(titleTxt, for: .normal)
                        btn.setTitleColor(tintColor, for: .normal)
                        btn.titleLabel?.font = font
                    }
                    btn.frame = CGRect.init(x: 0, y: 0, width: 35, height: 35)
                    btn.adjustsImageWhenHighlighted = false
                    let barButton = UIBarButtonItem.init(customView: btn)
                    if isLeft {
                        btn.addTarget(self, action: #selector(self.btnLeftNavigationClicked), for: UIControl.Event.touchUpInside)
                        btn.tintColor = tintColor
                        if let tabBar = self.tabBarController {
                            tabBar.navigationItem.leftBarButtonItem = nil
                            tabBar.navigationItem.leftBarButtonItem = barButton
                        } else {
                            self.navigationItem.leftBarButtonItem = nil
                            self.navigationItem.leftBarButtonItem = barButton
                        }
    
                    } else {
                        btn.addTarget(self, action: #selector(self.btnRightNavigationClicked), for: UIControl.Event.touchUpInside)
                        btn.tintColor = tintColor
                        if let tabBar = self.tabBarController {
                            tabBar.navigationItem.rightBarButtonItem = barButton
                        } else {
                            self.navigationItem.rightBarButtonItem = barButton
                        }
                    }
                }
            }
    

    Right button action

    @objc func btnRightNavigationClicked(sender: UIButton) { 
       // add your logic here         
    }
    

    Left button action

    @objc func btnLeftNavigationClicked(sender: UIButton) {
            guard let navigationController = self.navigationController else {
                return
            }
            navigationController.popViewController(animated: true)
    }
    
    0 讨论(0)
提交回复
热议问题