2010-08-16 10 views
14

में नेविगेशन बार में दायां बार बटन आइटम के रूप में जानकारी बटन जोड़ना क्या किसी को कोड में एक सूचना बटन (इटालिक 'i' सर्कल में) बनाने में कोई सफलता मिली है (पढ़ें: बिना इंटरफ़ेस बिल्डर के), और उसके बाद इसे असाइन करना नेविगेशन बार के दाएं बार बटन आइटम के रूप में?आईफोन: कोड

मैंने हर जगह देखा है, और मुझे यह भी नहीं पता कि कोड में जानकारी बटन कैसे बनाया जाए। किसी भी तरह की सहायता का स्वागत किया जाएगा।

उत्तर

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

शानदार, धन्यवाद रीना। मैंने इसे स्वीकार्य उत्तर के रूप में चिह्नित किया है, लेकिन अगर आप मेरी गाल को माफ करने के लिए बहुत दयालु होंगे, तो क्या मैं पूछ सकता हूं कि कोई जानकारी पिक्सेल को पांच पिक्सेल से कैसे छोड़ सकता है या नहीं? इसे सही बार बटन आइटम के रूप में जोड़ना मतलब है कि यह स्क्रीन के दाहिने तरफ से काफी कुचल जाता है। –

+1

'leftBarButtonItem' प्रॉपर्टी का उपयोग करके आप बाईं ओर self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: btn] पर सेट कर सकते हैं; – Reena

+1

क्षमा करें रीना। मैं इसे सही बार बटन आइटम के रूप में चाहता हूं, मैं बस दाईं ओर थोड़ा सा स्थान जोड़ना चाहता हूं (जिससे दाएं बटन 5px बाईं ओर लाया जा सकता है)। क्या यह संभव है? –

5

यहाँ एक बहुत अच्छी लग रही समाधान मुझे लगता है कि शामिल किया गया है सभी बिंदुओं लोगों के ऊपर टिप्पणी में लाया है। स्वीकार्य उत्तर के समान, लेकिन इस दृष्टिकोण में अतिरिक्त अंतर (फ्रेम को संशोधित करके) शामिल है ताकि बटन दाएं किनारे के खिलाफ टूट न जाए।

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

चीयर्स केली ने पूरी तरह से काम किया – adamteale

0

मुझे लगता है कि इसे और अधिक पूरा प्रतिक्रिया हो जाएगा और यह किसी भी स्थिति में मदद करनी चाहिए:

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

स्विफ्ट के लिए:

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 
}