Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否为工具栏按钮项设置图像和标题?_Ios_Swift_Uinavigationcontroller_Swift2 - Fatal编程技术网

Ios 是否为工具栏按钮项设置图像和标题?

Ios 是否为工具栏按钮项设置图像和标题?,ios,swift,uinavigationcontroller,swift2,Ios,Swift,Uinavigationcontroller,Swift2,我目前有一个自定义的导航控制器,它带有简单的文本按钮的栏按钮项。是否可以保留工具栏按钮项目的标题,但也可以将其设置为图像(图标图像+下面的标题) 更新: let button = UIButton(type: .System) button.setImage(UIImage(named: "play"), forState: .Normal) button.setTitle("Play", forState: .Normal) button.s

我目前有一个自定义的导航控制器,它带有简单的文本按钮的栏按钮项。是否可以保留工具栏按钮项目的标题,但也可以将其设置为图像(图标图像+下面的标题)

更新:

  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)
要添加操作,请执行以下操作:

    button.addTarget(self, action: #selector(self.someAction), forControlEvents: .TouchUpInside)
自我行动在哪里

func someAction() {

}
Swift 3:

    let button = UIButton(type: .system)
    button.setImage(UIImage(named: "categories_icon"), for: .normal)
    button.setTitle("Categories", for: .normal)
    button.addTarget(self, action: #selector(showCategories), for: .touchUpInside)
    button.sizeToFit()
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)

创建一个UIButton,为其设置一个图像和标题,并将其用作自定义图像,以初始化您的
UIBarButtonim(customView:)

如果希望图像位于按钮的右侧,可以将按钮的
语义内容属性设置为
。forceRightToLeft

Swift 4示例:

let view = UIView()
let button = UIButton(type: .system)
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "DocumentsIcon"), for: .normal)
button.setTitle("Documents", for: .normal)
button.addTarget(self, action: #selector(openDocuments), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)

谢谢我更新了我的问题。我怎样才能把动作分配给它呢?很好。谢谢你知道有没有办法把按钮标题放在按钮图片下方吗?@OlgaNesterenko当我使用你的解决方案时,图片是蓝色的。如何使图像保持原样而不变为蓝色?@ShinehahGnolaum尝试将第二行更改为
按钮。setImage(UIImage(名为:“YourImage”)。使用RenderingMode(.alwaysOriginal),forState:.Normal)
请解释您的代码行,以便其他用户了解其功能。谢谢
let view = UIView()
let button = UIButton(type: .system)
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "DocumentsIcon"), for: .normal)
button.setTitle("Documents", for: .normal)
button.addTarget(self, action: #selector(openDocuments), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)