2016-01-20 13 views
8

tôi cần phải thiết lập khung cho nút thanh điều hướng của tôi trong nhanh chóng 2,0Thay đổi chiều rộng của một UIBarButtonItem trong một UINavigationBar trong nhanh chóng

tôi đã cố gắng mã này

self.navigationController!.navigationBar.drawRect(CGRectMake(0, 0, 30, 30)) 

nhưng nó sẽ không làm việc

cảm ơn trước

+0

làm thế nào bạn thêm nút? – Johnykutty

+0

tôi chỉ muốn đặt nút bấm bằng khung hình, nhưng nó gọi phương thức khi tôi nhấp vào bên ngoài nút –

Trả lời

12
// Swift 3 
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 
backButton.setBackgroundImage(UIImage(named: "img"), for: .normal) 
backButton.addTarget(self, action: "action:", for: .touchUpInside) 
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton) 

// Swift 2 
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 
backButton.setBackgroundImage(UIImage(named: "img"), forState: .Normal) 
backButton.addTarget(self, action: "action:", forControlEvents: .TouchUpInside) 
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton) 
2
// We dont have Property to change UIBarButtonItem frame 
    // So we can creat UIButton() and give requered frame and add to navigationItem.setLeftBarButtonItems 
    // Please refere Belove code 

    // Swift 2.0 

    let btnBack = UIButton() 
    btnBack.frame = CGRectMake(0, 0, 100, 64) 
    btnBack.addTarget(self, action: "backAction", forControlEvents: UIControlEvents.TouchUpInside) 
    let leftBarBackBtn: UIBarButtonItem = UIBarButtonItem(customView: btnBack) 
    self.navigationItem.setLeftBarButtonItems([ leftBarBackBtn ], animated: false) 

    // Please submit your answer with Explanation comments to improve your Quality or Answer and question 
1

câu trả lời hàng đầu cho nhanh 3:

let homeButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20)) 
homeButton.setBackgroundImage(#imageLiteral(resourceName: "home-1"), for: .normal) 
homeButton.addTarget(self, action: #selector(homePressed), for: .touchUpInside) 
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: homeButton) 
0
func createFakeAndSearchCurrentLocationBarButton (vw: UIViewController) { 

    let fakeCurrentLocationGo = UIButton(type: .custom) 
    fakeCurrentLocationGo.setImage(UIImage(named: "reallocationgo50"), for: .normal) 
    fakeCurrentLocationGo.frame = CGRect(x: 0, y: 0, width: 20, height: 20) 
    fakeCurrentLocationGo.addTarget(vw, action: #selector(goToMyCurrentLocationPin), for: .touchUpInside) 
    let leftItem = UIBarButtonItem(customView: fakeCurrentLocationGo) 


    let searchLocationBtn = UIButton(type: .custom) 
    searchLocationBtn.setImage(UIImage(named: "search"), for: .normal) 
    searchLocationBtn.frame = CGRect(x: 0, y: 0, width: 15, height: 15) 
    fakeCurrentLocationGo.addTarget(vw, action: #selector(searchLocationHandle), for: .touchUpInside) 
    let rightItem = UIBarButtonItem(customView: searchLocationBtn) 

    vw.navigationItem.setRightBarButtonItems([leftItem,rightItem], animated: true) 

} 
+0

Vui lòng cập nhật để bao gồm giải thích cho khối mã này. –

Các vấn đề liên quan