ios - Set image and title for bar button item? -
i have custom navigation controller bar button items text buttons. possible keep title of bar button items set them 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 bar buttons text , 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:") }
you can create uibutton instance, set image , title it, , create uibarbuttonitem it:
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 action:
button.addtarget(self, action: #selector(self.someaction), forcontrolevents: .touchupinside)
where self.someaction is
func someaction() { }
Comments
Post a Comment