2016-04-19 31 views
5

Tôi hiện có bộ điều khiển điều hướng tùy chỉnh với các mục nút thanh chỉ đơn giản là nút văn bản. Có thể giữ tiêu đề của các mục nút thanh nhưng cũng đặt chúng thành hình ảnh (biểu tượng hình ảnh + tiêu đề bên dưới).Đặt hình ảnh và tiêu đề cho mục nút thanh?

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] 

    } 

Cập nhật:

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

Trả lời

17

Bạn có thể tạo UIButton dụ, thiết lập một hình ảnh và tiêu đề cho nó, và sau đó tạo ra UIBarButtonItem của bạn với nó:

let button = UIButton(type: .System) 
    button.setImage(UIImage(named: "YourImage"), forState: .Normal) 
    button.setTitle("YourTitle", forState: .Normal) 
    button.sizeToFit() 
    self.leftBarButton = UIBarButtonItem(customView: button) 

Để thêm một hành động :

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

whe tái self.someAction là

func someAction() { 

} 
+0

Cảm ơn! Tôi đã cập nhật câu hỏi của mình. Làm thế nào tôi có thể gán hành động cho nó? – winston

+0

Xem câu trả lời cập nhật. –

+0

Đã làm việc tuyệt vời. Cảm ơn! – winston

6

Swift 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) 
Các vấn đề liên quan