为什么 UIBarButtonItem 中的 UIButton 没有显示在导航栏中?

Why isn't my UIButton in a UIBarButtonItem not showing in the Nav Bar?

我有一个 UINavigationController,它会根据应用程序的状态更改右上角的 UIBarButtonItem

当视图加载时,我初始化我需要的按钮

var editBarButton: UIBarButtonItem!
var logoutBarButton: UIBarButtonItem!
var showRepositoryBarButton: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    editButton = UIButton()
    editButton.setImage(UIImage(named: "icon pencil"), forState: .Normal)
    editButton.frame = CGRectMake(0, 0, 30, 30)
    editButton.addTarget(self, action: #selector(UserProfileNavigatorController.onEditClick), forControlEvents: .TouchUpInside)
    editBarButton = UIBarButtonItem(customView: editButton)

    let logoutButton = UIButton()
    logoutButton.setImage(UIImage(named: "icon logout"), forState: .Normal)
    logoutButton.frame = CGRectMake(0, 0, 30, 30)
    logoutButton.addTarget(self, action: #selector(UserProfileNavigatorController.onLogoutClick), forControlEvents: .TouchUpInside)
    logoutBarButton = UIBarButtonItem(customView: logoutButton)

    let showRepositoryButton = UIButton()
    logoutButton.setImage(UIImage(named: "icon logout"), forState: .Normal)
    logoutButton.frame = CGRectMake(0, 0, 30, 30)
    logoutButton.addTarget(self, action: #selector(UserProfileNavigatorController.onShowRepositoryOnWebClick), forControlEvents: .TouchUpInside)
    showRepositoryBarButton = UIBarButtonItem(customView: showRepositoryButton)

    presenter.viewDidLoad()
}

并且根据情况,我改变了这样显示的按钮

func showUserProfile() {
    navigationItem.setRightBarButtonItems([editBarButton, logoutBarButton], animated: false)
    Router.showUserProfileInNavigatorController(self)
}

func showRepository(repository: Repository) {
    navigationItem.setRightBarButtonItems([showRepositoryBarButton], animated: false)
    Router.showRepository(self, repository: repository)
}

虽然实际执行了这段代码,但是右上角什么也没有出现。

请注意,您的代码存在多个问题。首先,请注意虽然 UIButton() 将 return 为非可选,但 UIImage 可能 return 为可选。所以你现在真的不知道你是否真的正确输入了你的图像名称。我使用上面的代码创建了一个简单的项目,其中没有图像,并复制了如果图像为零,则没有显示。

然后添加图片,我只显示了两张图片。仔细查看您的代码,第三张图片重新定义了 loginButton 但未在 showRepositoryButton 上设置任何属性。

几点建议:

  • 我们 "do { ... }" 创建子上下文以避免意外的变量重用
  • 如果使用 UIButton,请使用指定的初始化程序 (UIButton(type:...)
  • 更好的是,使用 UIBarButtonItem(image: ...)
  • 密切关注可选值,并在适当的时候让强制解包崩溃(或使用断言、保护或前提条件)

无需大幅修改您的代码,以下是适合我的方法:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Howdie!"

    do {
        let button = UIButton()
        let image = UIImage(named: "02-redo")!
        button.setImage(image, forState: .Normal)
        button.frame = CGRectMake(0, 0, 30, 30)
        button.addTarget(self, action: #selector(onEditClick), forControlEvents: .TouchUpInside)
        editBarButton = UIBarButtonItem(customView: button)
    }

    do {
        let button = UIButton()
        let image = UIImage(named: "03-loopback")!
        button.setImage(image, forState: .Normal)
        button.frame = CGRectMake(0, 0, 30, 30)
        button.addTarget(self, action: #selector(onLogoutClick), forControlEvents: .TouchUpInside)
        logoutBarButton = UIBarButtonItem(customView: button)
    }

    do {
        let button = UIButton()
        let image = UIImage(named: "04-squiggle")!
        button.setImage(image, forState: .Normal)
        button.frame = CGRectMake(0, 0, 30, 30)
        button.addTarget(self, action: #selector(onShowRepositoryOnWebClick), forControlEvents: .TouchUpInside)
        showRepositoryBarButton = UIBarButtonItem(customView: button)
    }

    navigationItem.setRightBarButtonItems([editBarButton, logoutBarButton, showRepositoryBarButton], animated: false)
}

问题是 UINavigationController 中的代码是 运行。相反,UIViewController 必须在 UINavigationController.

中显示按钮

因此只需将代码移入 UIViewController 即可。