swift2 是否设置栏按钮项的图像和标题?

wd2eg0qa  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(210)

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

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

k4emjkb11#

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

其中self.someAction为

func someAction() {

}
daolsyd0

daolsyd02#

创建一个UIButton,为它设置一个图像和一个标题,并使用它作为一个自定义图像来初始化您的UIBarButtonItem(customView:)
如果您希望图像位于按钮的右侧,可以将按钮的semanticContentAttribute设置为.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)
efzxgjgh

efzxgjgh3#

雨燕三:

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)

相关问题