我在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
}
答案 0 :(得分:0)
TableViewController
即MainMenu
MainMenu
控制器嵌入Navigation Controller
然后在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() {
}
}
为情节提要文件添加了步骤,
输出为