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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36730787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:56:37  来源:igfitidea点击:

Set image and title for bar button item?

iosswiftuinavigationcontrollerswift2

提问by winston

I currently have a custom navigation controller with bar button items that are simply text buttons. Is it possible to keep the title of the bar button items but also set them as images (icon image + title underneath).

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

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]

    }

Updated:

更新:

  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:")
        }

回答by Olga Nesterenko

You can create UIButton instance, set an image and a title for it, and then create your UIBarButtonItem with it:

您可以创建 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)

To add an action:

添加一个动作:

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

where self.someAction is

self.someAction 在哪里

func someAction() {

}

回答by alxhbly

Create an UIButton, set an image and a title for it and use it as a custom image to init your UIBarButtonItem(customView:)with it.

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

If you want the image to be on the right side of the button, you can set the button's semanticContentAttributeto .forceRightToLeft.

如果您希望图像位于按钮的右侧,您可以将按钮的设置semanticContentAttribute.forceRightToLeft

Swift 4 example:

斯威夫特 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)

回答by Dvole

Swift 3:

斯威夫特 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)