2010-08-16 40 views
14

Có ai đó đã tạo thành công nút thông tin (in nghiêng 'i' trong vòng kết nối) trong mã (đọc: không có Trình tạo giao diện) và sau đó gán làm mục nút thanh bên phải của thanh điều hướng?iPhone: Thêm nút Thông tin làm mục nút thanh bên phải trong thanh điều hướng trong mã

Tôi đã xem ở khắp mọi nơi và thậm chí tôi không thể tìm thấy cách tạo nút thông tin trong mã. Mọi sự trợ giúp sẽ rất được trân trọng.

Trả lời

32
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoDark]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; 
+0

Brilliant, nhờ Reena. Tôi đã đánh dấu nó là câu trả lời được chấp nhận, nhưng nếu bạn rất tử tế để tha thứ cho sự táo bạo của tôi, tôi có thể hỏi làm thế nào người ta có thể mang nút thông tin sang trái năm pixel hay không? Thêm nó như là mục nút thanh bên phải có nghĩa là nó được khá nghiền nát lên phía bên phải của màn hình. –

+1

sử dụng thuộc tính 'leftBarButtonItem' bạn có thể đặt ở bên trái self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: btn]; – Reena

+1

Xin lỗi Reena. Tôi muốn nó như là mục nút thanh bên phải, tôi chỉ muốn thêm một chút không gian bên phải (do đó mang nút phải 5px sang bên trái). Điều đó có thể không? –

5

Đây là giải pháp khá hay mà tôi nghĩ bao gồm tất cả các điểm mà mọi người đưa ra trong các nhận xét ở trên. Tương tự như câu trả lời được chấp nhận, nhưng cách tiếp cận này bao gồm khoảng cách thêm (thông qua việc sửa đổi khung hình) sao cho nút không bị đập vỡ lên cạnh phải.

// Create the info button 
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 

// Adjust the frame by adding an addition 10 points to its width so the button is padded nicely 
infoButton.frame = CGRectMake(infoButton.frame.origin.x, infoButton.frame.origin.y, infoButton.frame.size.width + 10.0, infoButton.frame.size.height); 

// Hook the button up to an action 
[infoButton addTarget:self action:@selector(showInfoScreen) forControlEvents:UIControlEventTouchUpInside]; 

// Add the button to the nav bar 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton]; 

// Also make sure to add a showInfoScreen method to your class! 
+0

cổ vũ Kyle làm việc hoàn hảo – adamteale

0

Tôi đoán nó sẽ được đáp ứng đầy đủ hơn và nó sẽ giúp đỡ trong mọi tình huống:

-(void)viewDidLoad{ 

    //Set Navigation Bar 
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)]; 

    //Set title if needed 
    UINavigationItem * navTitle = [[UINavigationItem alloc] init]; 
    navTitle.title = @"Title"; 

    //Here you create info button and customize it 
    UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 

    //Add selector to info button 
    [tempButton addTarget:self action:@selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 

    UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton]; 

    //In this case your button will be on the right side 
    navTitle.rightBarButtonItem = infoButton; 

    //Add NavigationBar on main view 
    navBar.items = @[navTitle]; 
    [self.view addSubview:navBar]; 

} 
1

Đối Swift:

func addRightNavigationBarInfoButton() { 
    let button = UIButton(type: .infoDark) 
    button.addTarget(self, action: #selector(self.showInfoScreen), for: .touchUpInside) 
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button) 
} 

@objc func showInfoScreen() { 
    // info bar button pressed 
} 
Các vấn đề liên quan