2009-03-02 13 views
101

मेरे पास तीन UIBarButtonItem नीचे दिए गए हैं। वे बाएं संरेखित करते हैं और मैं केंद्र को संरेखित करना चाहता हूं इसलिए दाएं तरफ कोई अंतर नहीं है। मुझे UIToolBar पर एक संरेखण संपत्ति दिखाई नहीं दे रही है। क्या इसे पूरा करने का कोई और तरीका है?UIToolBar आइटम संरेखित

//create some buttons 
UIBarButtonItem *aboutButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(showAbout:)]; 
[toolbar setItems:[NSArray arrayWithObjects:settingsButton,deleteButton,aboutButton,nil]]; 
//Add the toolbar as a subview to the navigation controller. 
[self.navigationController.view addSubview:toolbar]; 

उत्तर

244

इन आप के रूप में किसी अन्य उपकरण पट्टी आइटम उनमें से दो के बीच समान रूप से अंतरिक्ष वितरित करेंगे होगा जोड़ना छोड़ दिया और अपने आइटम

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, settingsButton,deleteButton,aboutButton, flexibleSpace, nil]]; 

के अधिकार के लिए अपनी उपकरण पट्टी करने के लिए दो UIBarButtonSystemItemFlexibleSpace आइटम जोड़ें।

+24

कोई वास्तविक कई लचीला रिक्त स्थान बनाने के लिए कारण नहीं है। यदि आपको एक से अधिक की आवश्यकता है तो आप अपने टूलबार पर कई बार लचीला स्पेस ऑब्जेक्ट जोड़ सकते हैं। – mmc

+0

एमएमसी सही है। वास्तव में, यह शायद सिंगलटन लचीली स्पेस बनाने और इसे अपने उत्पाद में पुन: उपयोग करने के लिए पूरी तरह से उचित होगा। मैंने सुना है कि यह दावा किया गया है कि यह सिंगलेट का असली मूल उपयोग था: प्रोग्राम नियमों को लागू नहीं करना, बल्कि ओवरहेड को कम करना। – Amagrammer

+0

लचीली जगह – nduplessis

6

Xamarin आईओएस में

राइट गठबंधन:

yourBar.SetItems(new [] { new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), yourButton }, false); 

केंद्र निरपेक्ष:

var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace); 
yourBar.SetItems(new [] { flexibleSpace, yourButton, flexibleSpace}, false); 
+4

यह विषय नहीं है। हम यहां शुद्ध आईओएस के बारे में बात कर रहे हैं। – jturolla

+4

मोनो टच लक्ष्य क्या है। –

+2

उद्देश्य सी प्रश्न मोनो टच/सी # –

25

यह भी एक स्टोरीबोर्ड से सही किया जा सकता है।

बस टूलबार में आइटम खींचें और छोड़ें, और वांछित प्रभाव प्राप्त करने के लिए उनमें से कुछ को लचीला या निश्चित स्थान में बदलें। नीचे दो उदाहरण देखें।

Evenly spaced

Centered

+0

मैं अपने यूआई को प्रबंधित करने के लिए स्टोरीबोर्ड का उपयोग करना पसंद करता हूं। – AechoLiu

2

स्विफ्ट संस्करण:

let toolbar = UIToolbar(frame: CGRectMake(0, 0, viewController.view.frame.size.width, 35.0)) 
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: viewController, action: nil) 
    let button1 = UIBarButtonItem(title: "A", style: UIBarButtonItemStyle.Plain, target: viewController, action: foo) 
    let button2 = UIBarButtonItem(title: "B", style: UIBarButtonItemStyle.Plain, target: viewController, action: bar) 
    let button3 = UIBarButtonItem(title: "C", style: UIBarButtonItemStyle.Plain, target: viewController, action: blah) 
    toolbar.items = [button1, flexibleSpace, button2, flexibleSpace, button3] 
संबंधित मुद्दे