我目前有一个带有条形按钮项目的自定义导航控制器,它们只是文本按钮。是否可以保留条形按钮项目的标题,但也可以将它们设置为图像(图标图像+标题下方)。
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:")
}
答案 0 :(得分:37)
您可以创建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() {
}
答案 1 :(得分:8)
斯威夫特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)
答案 2 :(得分:7)
创建一个UIButton,为其设置图像和标题,并将其用作自定义图像,以使用它来初始化 byte[] bytes = { 1,2,3,4 };
string stringByte= BitConverter.ToString(bytes);
Console.WriteLine(stringByte);
。
如果您希望图片位于按钮的右侧,则可以将按钮的UIBarButtonItem(customView:)
设置为semanticContentAttribute
。
Swift 4例子:
.forceRightToLeft