2012-06-11 12 views
13

मैं एक UIBarButtonItem बनाने और इतने की तरह मेरे नेविगेशन पट्टी में जोड़ने से कर रहा हूँ:बदलें

(void)viewDidLoad { 

    ... 

    // Add the refresh button to the navigation bar 
    UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [refreshButton setFrame:CGRectMake(0,0,30,30)]; 
    [refreshButton setImage:[UIImage imageNamed:@"G_refresh_icon.png"] forState:UIControlStateNormal]; 
    [refreshButton addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease]; 
    self.navigationItem.leftBarButtonItem = refreshBarButton; 
} 

जब मैं चलाने के सही लगती है, लेकिन मैं दोहन से बार बटन आइटम का चयन कर सकते हैं नेविगेशन बार कहीं भी x = 0 से लगभग 100 तक। मैं 30 पीएक्स की चौड़ाई रखने के लिए चुनिंदा क्षेत्र को कैसे समायोजित कर सकता हूं?

+2

मुझे डर है कि मुझे नहीं लगता कि चयन क्षेत्र को समायोजित करने के लिए एक रास्ता है हूँ। एक सवाल हालांकि, आप चौड़ाई 30 पीएक्स क्यों चाहते हैं? आईओएस मानव इंटरफेस दिशानिर्देश बताता है कि टापेबल यूआई तत्वों का आरामदायक न्यूनतम आकार 44 x 44 अंक है। http://developer.apple।कॉम/लाइब्रेरी/आईओएस/# प्रलेखन/उपयोगकर्ता अनुभव/संकल्पनात्मक/मोबाइल एचआईजी/लक्षण/विशेषताएँ –

+0

हम्म, ठीक दिशानिर्देशों के लिंक के लिए धन्यवाद। यह थोड़ा अजीब लगता है कि चुनिंदा क्षेत्र इतना व्यापक है। – Darren

+0

@AbdSaniAbdJalal क्या हम स्टोरीबोर्ड में 'barbuttonitem' की चौड़ाई बदल सकते हैं? – aircraft

उत्तर

12

डैरेन,

एक दृष्टिकोण पर विचार कर सकते initWithCustomView को फोन करके एक UIBarButtonItem पैदा कर रही है। यह आदर्श नहीं है कि आपको बॉक्स से "चयनित" राज्य नहीं मिलते हैं और आपको अपनी बटन छवि के साथ अपनी सीमाबद्ध पृष्ठभूमि (यदि वह दिखना है) को जोड़ना है, लेकिन इसके साथ आप सीधे अपने लिए एक फ्रेम निर्दिष्ट कर सकते हैं टूलबार आइटम यदि आप छवियों के बजाय अपने शीर्षक के लिए टेक्स्ट का उपयोग कर रहे हैं तो आपको अभी भी एक पृष्ठभूमि छवि में एक सबव्यू के रूप में जोड़ने की आवश्यकता हो सकती है। वैसे भी, मैं एक ही समस्या आ रही है अभी और इस कोड को मेरे लिए काम करता है:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button-image.png"]]; 
imageView.frame = CGRectMake(0, 0, 43, 30); 

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView]; 

self.navigationItem.leftBarButtonItem = barButtonItem; 

अभी इस एक ही रास्ता मैं UIBarButtonItems की स्वतः-आकार को सीमित करने के बारे में पता UINavigationController के navigationItem को जोड़ा गया है ।

[संपादित]

या try Maggie's solution, which is more thorough than mine

+2

केवल तभी काम करता था जब मैंने 'UIButton' को कस्टम व्यू के रूप में उपयोग किया था, और' UIImageView' – Kreutzer

+1

नहीं, यह समाधान काम नहीं करेगा क्योंकि इसमें बटन क्रियाएं नहीं हैं। इसके बजाए 'UIButton' का प्रयोग करें। – Raptor

6

आप इंटरफ़ेस बिल्डर से बार बटन आइटम के लिए एक छवि को छोड़ने और इस कोड के साथ कस्टम दृश्य की चौड़ाई बदलने के द्वारा यह कर सकते हैं:

CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame; 
frame.size.width = 141; 
self.navigationItem.leftBarButtonItem.customView.frame = frame; 
5

इस के साथ मुख्य बात को एहसास है कि आप है UIBarButton के बजाय, कस्टम व्यू की चौड़ाई बदल रहे हैं।

कोड इसलिए है: कह रही है कि लेआउट ऑटो आकार देना और फ्रेम के साथ किया जा रहा है बिना

[myNavigationBar setNeedsLayout]; 

यह सब हो जाता है:

CGRect resizedFrame = myBarButtonItem.customView.frame; 
resizedFrame.size.width = myNewWidth; 
myBarButtonItem.customView.frame = resizedFrame; 

तुम भी लेआउट परिवर्तन को गति प्रदान करने की आवश्यकता होगी। ऑटो लेआउट के साथ नेविगेशन बार में घुसपैठ कोई सफलता नहीं मिली। मेरा प्रश्न Auto Layout with UINavigationBar and UIBarButtonItem देखें।

क्षमा करें बस एहसास हुआ कि मेरा कोड लगभग @oscartzombie के समान है। जानबूझकर नहीं! मैं इस जवाब को छोड़ दूंगा क्योंकि मुझे लगता है कि लेआउट और अन्य बिंदुओं को जोड़ने के लायक है, इसके अलावा इंटरफ़ेस बुलर या छवियों के संदर्भ के बिना विशेष रूप से व्याख्या करना।

+0

मुझे अपने कस्टम UIBarButtonItem के आकार के साथ समस्याएं थीं, समाधान मेरे दृश्य नियंत्रक के '- (शून्य) viewDidLoad' विधि में' [self.navigationController.navigationBar setNeedsLayout] 'को कॉल कर रहा था। – LunaCodeGirl

5

निम्नलिखित दृष्टिकोण काम किया, बटन की चौड़ाई और ऊंचाई बदलने के लिए कोड देखें और साथ ही कार्रवाई आइटम भी जोड़ें।

UIButton *imageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)]; 
[imageButton setBackgroundImage:[UIImage imageNamed:@"message_button"] forState:UIControlStateNormal]; 
[imageButton addTarget:self action:@selector(messageButtonTapped) forControlEvents:UIControlEventAllEvents]; 
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton]; 
self.navigationItem.leftBarButtonItem = leftBarButtonItem; 

नोट: लक्ष्य और UIBarButtonItem की कार्रवाई आकृति दृश्य पर लागू नहीं होता है

संबंधित मुद्दे