2012-11-12 12 views
6

Tôi muốn đặt nút lập trình cho navigationItem.titleView.Cách đặt nút cho navigationItem titleView?

Tôi đã thử với mã sau nhưng không thành công;

UIBarButtonItem *navigationTitleButton = [[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStyleBordered target:self action:@selector(backToHOmePage)]; 

navigationTitleButton.image = [UIImage imageNamed:@"title.png"]; 

self.navigationItem.titleView = navigationTitleButton; 

Warnng nói: các loại con trỏ không tương thích gán cho 'UIView *' từ 'UIBarButtonItem * __ mạnh'

Trả lời

13

Cố gắng sử dụng mã này xem nếu nó làm việc cho bạn

UIView * container = [[UIView alloc]initWithFrame:CGRectZero]; 
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero]; 
[button addTarget:self action:@selector(backToHOmePage) forControlEvents:UIControlEventTouchUpInside]; 
[button setImage:[UIImage imageNamed:@"clanak_default.png"] forState:UIControlStateNormal]; 
[button sizeToFit]; 
[container addSubview:button]; 
[button release]; 
[container sizeToFit]; 
self.navigationItem.titleView = container; 
[container release]; 
+0

Tuyệt vời. Bây giờ tôi hiểu nơi tôi đã sai. Cảm ơn bạn. – CroiOS

+7

Giải pháp của bạn hoạt động, cảm ơn! Tại sao nó là nếu UIButton là một phân lớp của UIView, rằng một UIButton không thể được sử dụng trực tiếp như titleView? – cph2117

+0

Nó sẽ làm việc với một UIButton, UILabel, vv Bạn chỉ cần initWithFrame một kích thước có thể nhìn thấy UIView. Xem bên dưới hoặc trong câu trả lời của Evgenjy S. – Chucky

0

Hãy thử tạo một đối tượng UIButton cho nút của bạn thay vì một UIBarButtonItem một. UIButton là một lớp con của UIView, do đó bạn sẽ có thể đặt thuộc tính titleView cho UIButton mà bạn đã tạo.

10

Bạn không phải tạo BarItem, bạn cần tạo đối tượng lớp UIView;

ARC

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; 
view.backgroundColor = [UIColor clearColor]; 

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
button.frame = view.frame; 

[view addSubview:button]; 

self.navigationItem.titleView = view; 

hoặc

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(0, 0, 40, 40); 
    self.navigationItem.titleView = button; 
0

Bạn có thể thêm nút bấm của bạn trên một cái nhìn vậy thì hãy chờ xem như quan điểm tựa đề của thanh điều hướng thì bạn có thể thay đổi tiêu đề của nút lúc chạy thời gian dễ dàng.

2

Swift phiên bản:

var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton 
button.frame = CGRectMake(0, 0, 100, 40) as CGRect 
button.backgroundColor = UIColor.redColor() 
button.setTitle("Button", forState: UIControlState.Normal) 
button.addTarget(self, action: Selector("clickOnButton:"), forControlEvents: UIControlEvents.TouchUpInside) 
self.navigationItem.titleView = button 

Ở đây bạn có thể thấy trong nút dòng cuối cùng là trực tiếp thiết lập để titleView của navigationItem mà sẽ thêm nút ở trung tâm của thanh điều hướng.

phương pháp hành động cho nút dưới:

func clickOnButton(button: UIButton) { 

} 
2

Bạn có thể thêm một UIImageView như titleView của navigationItem trực tiếp, tuy nhiên nó sẽ không được hiển thị nếu bạn không chắc chắn rằng nó có kích thước. Cách dễ nhất để kích thước nó là sử dụng sizeToFit().

Trong ví dụ này, tôi cũng làm tròn các góc và thêm khả năng để biết nếu nó được khai thác.

override func viewDidLoad() { 
    super.viewDidLoad() 

    let titleIconButton = UIButton(type: .Custom) 
    titleIconButton.setImage(UIImage(named: "login-icon"), forState: .Normal) 
    titleIconButton.addTarget(self, action: #selector(titleIconTap), forControlEvents: .TouchUpInside) 
    titleIconButton.clipsToBounds = true 
    titleIconButton.layer.cornerRadius = 4 
    titleIconButton.sizeToFit() 
    navigationItem.titleView = titleIconButton 
} 

enter image description here

1

Swift 3 Version:

let logoButton = UIButton(type: .custom) 
    logoButton.setImage(#imageLiteral(resourceName: "Logo"), for: .normal) 
    logoButton.sizeToFit() 
    logoButton.addTarget(self, action: #selector(tapLogo), for: .touchUpInside) 

    self.navigationItem.title = "" 
    self.navigationItem.titleView = logoButton 
0

Nếu bạn có kế hoạch thay đổi button.title lúc chạy, và bạn thay đổi tiêu đề để văn bản dài hơn, nó sẽ không được làm trung tâm. Và nếu bạn làm button.sizeToFit(), nó sẽ được căn giữa, nhưng hiển thị tiêu đề mới là "T..e".

Giải pháp là đặt UIButton vào chế độ UIView. Và hạn chế nút cho tất cả các mặt của UIView. Sau đó, chỉ cần đặt navigationItem.titleView thành UIView đó. Và nó sẽ xử lý các thay đổi tiêu đề có lập trình một cách hoàn hảo.

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