【问题标题】:Add custom button to navigation controller without border将自定义按钮添加到无边框导航控制器
【发布时间】:2011-08-03 00:45:24
【问题描述】:

我将自定义按钮添加到导航控制器

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] 样式:UIBarButtonItemStylePlain 目标:自己 动作:@selector(backAction)]; self.navigationItem.leftBarButtonItem = backButton;

它工作正常,但按钮出现边框。我该如何解决?

更新 我找到了解决方案

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; [按钮 setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button]; [按钮释放];
self.navigationItem.leftBarButtonItem = back;

【问题讨论】:

    标签: xcode ios uinavigationcontroller uibarbuttonitem


    【解决方案1】:

    试试这个。

    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];
    

    【讨论】:

      【解决方案2】:

      对于 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)
      

      动作(选择器)应该如下所示:

      @objc func backAction () {
              // do the magic 
      }
      

      【讨论】:

        【解决方案3】:

        根据需要在左右添加带标题或不带标题的按钮,

        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
                            }
                        }
                    }
                }
        

        右键操作

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

        左键操作

        @objc func btnLeftNavigationClicked(sender: UIButton) {
                guard let navigationController = self.navigationController else {
                    return
                }
                navigationController.popViewController(animated: true)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-15
          • 2021-06-27
          • 1970-01-01
          • 2018-06-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多