我的TableViewController中没有看到导航栏按钮项目

时间:2018-12-31 00:48:17

标签: ios uitableview

我在main.storyboard中创建了一个UITableViewController,然后将其与名为MainMenu的类链接起来。在该类中,我手动添加了UIBarButtonItems以添加朋友并注销,但是我什么也没看到。我所看到的只是带有灰色线条的空白表格视图。

 class MainMenu: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()



    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))

    let image = UIImage(named: "add_friend_image")


    let menuButton = UIButton(type: .custom)
    menuButton.frame = CGRect(x: 0.0, y: 0.0, width: 20, height: 20)
    menuButton.setImage(image, for: .normal)
    menuButton.addTarget(self, action: #selector(handleFriend), for: UIControlEvents.touchUpInside) 

    let menuBarItem = UIBarButtonItem(customView: menuButton)
    let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 24)
    currWidth?.isActive = true
    let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 24)
    currHeight?.isActive = true;
    self.navigationItem.rightBarButtonItem = menuBarItem



}

1 个答案:

答案 0 :(得分:0)

  1. 创建一个情节提要,然后添加您的TableViewControllerMainMenu
  2. MainMenu控制器嵌入Navigation Controller
  3. 将导航控制器设置为初始视图控制器
  4. 然后在viewDidLoad类的MainMenu中添加您的代码以设置导航栏按钮项

    class MainMenu: UITableViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
    
            let image = UIImage(named: "add_friend_image")
    
    
            let menuButton = UIButton(type: .custom)
            menuButton.frame = CGRect(x: 0.0, y: 0.0, width: 20, height: 20)
            menuButton.setImage(image, for: .normal)
            menuButton.addTarget(self, action: #selector(handleFriend), for: .touchUpInside)
    
            let menuBarItem = UIBarButtonItem(customView: menuButton)
            let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 24)
            currWidth?.isActive = true
            let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 24)
            currHeight?.isActive = true;
            self.navigationItem.rightBarButtonItem = menuBarItem
        }
    
        @objc func handleLogout() {
    
        }
    
        @objc func handleFriend() {
    
        }
    }
    

为情节提要文件添加了步骤,

Image 1 Image 2 Image 3

输出为

output