我目前有一个自定义的导航控制器,其中的栏按钮项是简单的文本按钮。是否可以保留栏按钮项的标题,但也将其设置为图像(图标图像+标题下方)。
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:")
}
3条答案
按热度按时间k4emjkb11#
您可以创建UIButton示例,为它设置图像和标题,然后使用它创建UIBarButtonItem:
要添加操作,请执行以下操作:
其中self.someAction为
daolsyd02#
创建一个UIButton,为它设置一个图像和一个标题,并使用它作为一个自定义图像来初始化您的
UIBarButtonItem(customView:)
。如果您希望图像位于按钮的右侧,可以将按钮的
semanticContentAttribute
设置为.forceRightToLeft
。Swift 4示例:
efzxgjgh3#
雨燕三: