2013-10-21 12 views
6

में एक UIToolBar के लिए एक शीर्षक की स्थापना मैं एक UIToolBar बटन के नीचे घोषित किया है:आईओएस 7

UIToolbar *toolbar = [[UIToolbar alloc] init]; 
toolbar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 64.0); 

मैं भी एक UIBarButtonItem है कि एक वापस बटन के रूप में छोड़ दिया पर में जोड़ा जाता है। लेकिन मैं सिर्फ UIToolbar में एक केंद्रित लेबल शीर्षक कैसे जोड़ूं?

उत्तर

7
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 20)]; 
lblTitle.backgroundColor = [UIColor clearColor]; 
lblTitle.textColor = [UIColor blackColor]; 
lblTitle.textAlignment = NSTextAlignmentLeft; 
[self.view addSubview:lblTitle]; 
UIBarButtonItem *typeField = [[UIBarButtonItem alloc] initWithCustomView:lblTitle]; 
toolBar.items = [NSArray arrayWithArray:[NSArray arrayWithObjects:backButton,spaceBar,lblTitle, nil]]; 

हो जाएगा ताकि मुझे लगता है कि अपनी समस्या का समाधान करें।

6

एक UILabel बनाएँ और उपकरण पट्टी के एक आइटम के रूप में जोड़ें:

UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:myLabel]; 

आप इसे केंद्र पक्षों पर लचीला रिक्त स्थान जोड़ना चाहते हैं:

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] 
+0

मैं लेबल के रूप में इस राशि : UILabel * errorLabel = [[UILabel alloc] initWithFrame: CGRectMake (100.0, 100.0, 140.0, 125.0)]; लेकिन आयत की वाई संपत्ति सही काम नहीं कर रही है – cdub

+0

आपकी टूलबार 64 ऊंचाई है, और यदि आपका लेबल 100 से शुरू होता है, तो आपको y को बदलने की आवश्यकता है। –

+0

हाँ, लेकिन यूआईएलएबल टेक्स्ट मेरे UIBarButtonItem बैक बटन की तुलना में बंद है। मैं UILabel टेक्स्ट को पृष्ठ – cdub

1

UIToolbar में कोई शीर्षक संपत्ति नहीं है। जैसा कि अन्य लोगों ने सुझाव दिया है कि आपको एक शीर्षक के रूप में UILabel जोड़ने की आवश्यकता है।

या आप के बजाय UINavigationBar का उपयोग करें और अन्य उत्तर कॉपी के रूप में थे और pasteable के रूप में मैं चाहता था की - (id)initWithTitle:(NSString *)title;

4

कोई नहीं का उपयोग कर प्रारंभ कर सकते हैं, तो ...

UILabel *toolbarLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
toolbarLabel.text = @"Text in yo toolbar"; 
[toolbarLabel sizeToFit]; 
toolbarLabel.backgroundColor = [UIColor clearColor]; 
toolbarLabel.textColor = [UIColor grayColor]; 
toolbarLabel.textAlignment = NSTextAlignmentCenter; 
UIBarButtonItem *labelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel]; 
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
[self setToolbarItems:@[flexible, labelItem, flexible] animated:false]; 

इसका मतलब यह है तुम हो टूलबार आपके नेविगेशन नियंत्रक के माध्यम से दिखा रहा है (किसी नेविगेशन नियंत्रक द्वारा दिखाए गए किसी भी व्यू कंट्रोलर से इसे दिखाने के लिए, [self.navigationController setToolbarHidden:NO animated:YES];)

यह somethin दिखता है इस तरह जी (आईओएस 9): UILabel in a UIBarButtonItem with a custom view

+0

अच्छा जवाब, धन्यवाद। आपको 10 अंक बोनस दिया (अन्य उत्तर पर)। –

1

यहाँ हारून ऐश के जवाब का एक स्विफ्ट 2.2 कार्यान्वयन है

let toolbarLabel = UILabel(frame: CGRectZero) 
toolbarLabel.text = "Text in yo toolbar" 
toolbarLabel.sizeToFit() 
toolbarLabel.backgroundColor = UIColor.clearColor() 
toolbarLabel.textColor = UIColor.grayColor() 
toolbarLabel.textAlignment = .Center 
let labelItem = UIBarButtonItem.init(customView: toolbarLabel) 
let flexible = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target:nil, action:nil) 
self.setToolbarItems([flexible, labelItem, flexible], animated: false) 
0

यहाँ है एक स्विफ्ट 3 कार्यान्वयन:

let topBarTitleLabel = UILabel.init(frame: (CGRect.init(origin: CGPoint.init(x: 0.0, y: 0.0), size: CGSize.init(width: 0.0, height: 0.0)))) 
    topBarTitleLabel.text = "Text in yo toolbar" 
    topBarTitleLabel.sizeToFit() 
    topBarTitleLabel.backgroundColor = UIColor.clear 
    topBarTitleLabel.textColor = UIColor.black 
    topBarTitleLabel.textAlignment = NSTextAlignment.center 
    let topBarButtonItemTitleLabel = UIBarButtonItem.init(customView: topBarTitleLabel) 
    let flexibleBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) 
    self.topToolBar.setItems([flexibleBarButtonItem, topBarButtonItemTitleLabel, flexibleBarButtonItem], animated: false) 
    self.topToolBar.setNeedsLayout()