2012-10-13 13 views
12

मैं प्रोग्राम के रूप में एक उपकरण पट्टी बनाया:टूलबार के दाईं ओर एक बटन कैसे जोड़ें?

UIToolbar *boolbar = [UIToolbar new]; 
    boolbar.barStyle = UIBarStyleDefault; 
    boolbar.tintColor = [UIColor orangeColor]; 
    [boolbar sizeToFit]; 

और फिर इसे करने के लिए एक बटन कहा:

UIBarButtonItem *cancelleftBarButton =[[UIBarButtonItem alloc]initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(tapBackGround:)]; 

cancelleftBarButton.tintColor = [UIColor orangeColor]; 

NSArray *array = [NSArray arrayWithObjects:cancelleftBarButton, nil]; 
[boolbar setItems:array animated:YES]; 

हालांकि, इस बटन केवल उपकरण पट्टी के बाईं ओर दिखाई देता है। क्या टूलबार के दाहिने तरफ इसे रखना संभव है?

enter image description here

उत्तर

34

यहाँ उपकरण पट्टी के दाईं ओर UIBarButtonItem जोड़ने के लिए तरीका है।

UIBarButtonItem *leftButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem1Pressed:)] autorelease]; 

UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease]; 

UIBarButtonItem *rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem2Pressed:)] autorelease]; 

या

आप XIB से यह तो ऐसा करने के लिए, प्रयास कर रहे हैं।

एक आइटम डालें जिसमें पहचानकर्ता "लचीली जगह" है।

enter image description here

+0

इसके अलावा इन कड़ियों की जाँच http://stackoverflow.com/questions/602717/aligning-uitoolbar-items और http://stackoverflow.com/ प्रश्न/6021138/कैसे करने वाली समायोजित-uitoolbar-बाएँ और दाएँ-गद्दी – IronManGill

4

स्विफ्ट में

let btn1 = UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Done, target: self, action: "btn1Pressed" 
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
let btn2 = UIBarButtonItem(title: "Button 2", style: UIBarButtonItemStyle.Done, target: self, action: "btn2Pressed") 
संबंधित मुद्दे