2010-08-16 21 views

cevap

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

Parlak, teşekkür Reena. Onu kabul edilen cevap olarak işaretledim, ama eğer yanağımı bağışlayacak kadar nazik olsaydın, bilgi butonunu beş pikselle sola nasıl sorabilirim diye sorabilir miyim? Doğru çubuk düğme öğesi olarak eklenmesi, ekranın sağ tarafına doğru ezildiğini gösterir. –

+1

'leftBarButtonItem' özelliğini kullanarak sol olarak ayarlayabilirsiniz. Self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: btn]; – Reena

+1

Üzgünüm Reena. Ben sağ çubuk düğme öğesi olarak istiyorum, sadece sağda biraz boşluk eklemek istiyorum (böylece sağ tuşa 5px getirerek). Mümkün mü? –

5

Burada, yukarıdaki yorumlarda dile getirilen tüm noktaları kapsayan oldukça iyi görünen bir çözüm var. Kabul edilen cevaba benzer, ancak bu yaklaşım, düğmenin sağ kenara çarpmaması için ekstra boşluk (çerçeveyi değiştirerek) içerir.

// 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

Şerefe Kyle mükemmel çalıştı – adamteale

0

ben daha tam yanıt olacak tahmin ve her durumda yardımcı olacaktır:

-(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

Swift İçin:

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 
} 
İlgili konular