ios - 为条形按钮项目设置图像和标题?

标签 ios swift uinavigationcontroller swift2

我目前有一个自定义导航 Controller ,其中的条形按钮项只是文本按钮。是否可以保留栏按钮项的标题,但也可以将它们设置为图像(图标图像 + 下面的标题)。

class NavigationController: UINavigationController
{
    var mode: NavigationMode = .Swipe {
        didSet {
            self.setButtonAttributes()
        }
    }

    private var leftBarButton: UIBarButtonItem!
    private var middleBarButton: UIBarButtonItem!
    private var rightBarButton: UIBarButtonItem!
    private var rightBarButton2: UIBarButtonItem!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    func configureNavigationItem(navigationItem: UINavigationItem)
    {
        //Configure the bar buttons text and actions


        if (self.leftBarButton == nil) {
            self.leftBarButton = UIBarButtonItem(title: "Menu1", style: .Plain,target: self, action: "menu1Pressed:")
        }
        if (self.middleBarButton == nil) {
            self.middleBarButton = UIBarButtonItem(title: "Games", style: .Plain, target: self, action: "gamesPressed:")
        }
        if (self.rightBarButton == nil) {
            self.rightBarButton = UIBarButtonItem(title: "Menu3", style: .Plain, target: self, action: "menu3Pressed:")
        }
        if (self.rightBarButton2 == nil) {
            self.rightBarButton2 = UIBarButtonItem(title: "Settings", style: .Plain, target: self, action: "settingsPressed:")
        }

        self.setButtonAttributes()

        navigationItem.leftBarButtonItems = [self.leftBarButton, self.middleBarButton, self.rightBarButton, self.rightBarButton2]

    }

更新:

  let button = UIButton(type: .System)
        button.setImage(UIImage(named: "play"), forState: .Normal)
        button.setTitle("Play", forState: .Normal)
        button.sizeToFit()

        leftBarButton = UIBarButtonItem(customView: button)

        if (self.leftBarButton == nil) {
            self.leftBarButton = UIBarButtonItem(title: "Play", style: .Plain,target: self, action: "Pressed:")
        }

最佳答案

您可以创建 UIButton 实例,为其设置图像和标题,然后使用它创建 UIBarButtonItem:

    let button = UIButton(type: .System)
    button.setImage(UIImage(named: "YourImage"), forState: .Normal)
    button.setTitle("YourTitle", forState: .Normal)
    button.sizeToFit()
    self.leftBarButton = UIBarButtonItem(customView: button)

添加一个 Action :

    button.addTarget(self, action: #selector(self.someAction), forControlEvents: .TouchUpInside)

self.someAction 在哪里

func someAction() {

}

关于ios - 为条形按钮项目设置图像和标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36730787/

相关文章:

ios - 通过 iOS 将视频上传到 Amazon S3

iphone - 使用 UINavigationController 和默认后退按钮时如何将数据传回父 View ?

iphone - 如何为Person ViewController的导航栏添加返回按钮?

ios - segue 和 NavigationController 导航有什么问题

ios - SWRevealViewController 中的推送 View 错误

iphone - 使用 NSRegularExpression 查找和替换

ios - 在ARKit 1.5中进行初始检测后如何跟踪图像 anchor ?

ios - 语音 : Read items of table cell separately - not as a whole

ios - 从多个 View 访问 NSUserDefault

java - 小程序 - 将读取/修改系统文件并执行系统命令