2014-10-14 4 views
6

के साथ एक UITabBarItem पर किसी आसान तरीका लेबल की स्थापना मैं बहुत की तरह एक UITabBarItem है:कोई शीर्षक

_Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:nil tag:0]; 

लेकिन एक शीर्षक के लिए नहीं के बराबर होने लेबल पहुंच और KIF परीक्षण के लिए आवश्यक निकाल देता है। एक वैकल्पिक मैंने पाया शीर्षक सेट और इसे बंद को स्थानांतरित करने के लिए स्क्रीन है, लेकिन यह एक hacky समाधान की तरह लगता है:

_Controller.tabBarItem.title = @"Foo"; 
_Controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 200); 

क्या यह संभव है एक शीर्षक के बिना एक UITabBarItem है, लेकिन अभी भी किसी आसान तरीका लेबल है?

- (void) loadViewController { 
    _Controller = [[UIViewController alloc] init]; 
    UIImage *normalImage = [UIImage imageNamed:@"bar.png"]; 
    UIImage *selectedTabImage = [UIImage imageNamed:@"barHover.png"]; 
    [self addCenterButtonWithImage:normalImage 
        highlightImage:selectedTabImage]; 

    _Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:nil tag:0]; 
} 

// Create a custom UIButton and add it to the center of our tab bar 
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage 
{ 
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); 
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted]; 
    [button addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside]; 

    button.center = CGPointMake(self.tabBar.frame.size.width/2.0, self.tabBar.frame.size.height/2.0 - 6.0); 

    [self.tabBar addSubview:button]; 
} 

उत्तर

10

iOS8 में, आप एक टैब बार आइटम को सीधे किसी आसान तरीका लेबल असाइन कर सकते हैं:

_Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:nil tag:0]; 
_Controller.tabBarItem.accessibilityLabel = @"Foo"; 

iOS7 के लिए और

संपादित करें टैब बार और पृष्ठभूमि बटन कोड के लिए पूर्ण कोड जोड़ने के लिए नीचे, आप सही हैं कि आपको टेक्स्ट को छिपाने के लिए कुछ करने की ज़रूरत है। आप इसे गुप्त मजबूर कर सकते हैं जैसे आप सचित्र था:

_Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Foo" image:nil tag:0]; 
_Controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 200); 

या आप पाठ का रंग स्पष्ट कर सकते हैं:

_Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Foo" image:nil tag:0]; 
[_Controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor clearColor]} forState:UIControlStateNormal]; 

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

button.isAccessibilityElement = NO; 
button.userInteractionEnabled = NO; 
+1

यह मेरे लिए काम नहीं करता है। ऐसा लगता है कि UITabBarItem के पीछे बटन में इसके फ़ाइल नाम (एक्सटेंशन के बिना) पर एक एक्सेसिबिलिटी लेबल सेट है, न कि लेबल जो मैंने सेट किया है। यह अभी भी UITabBarItem के पीछे है और जैसा कि केआईएफ द्वारा टापेबल नहीं है। संपादित करें: मुझे लगता है कि मुझे उल्लेख किया जाना चाहिए कि UITabBarItem के पीछे एक छवि है। – jjj

+1

हम्म ... ऐसा लगता है कि एक्सेसिबिलिटी लेबल आईओएस 8 में काम करता है लेकिन आईओएस 7 नहीं। टैब बार आइटम के पीछे आप छवि को कैसे सम्मिलित कर रहे हैं? क्या आप फ़ाइल नाम का उपयोग एक्सेसिबिलिटी लेबल के रूप में कर कर टैब टैप कर सकते हैं? –

+0

मैंने मूल पोस्ट पर बटन के लिए पूरा कोड जोड़ा है। यदि मैं फ़ाइल नाम लेबल का उपयोग करने का प्रयास करता हूं तो मुझे वही त्रुटि मिलती है जब मैं 'addCenterButtonWithImage'' के अंदर बटन में लेबल जोड़ने का प्रयास करता हूं: 'लेबल के साथ अभिगम्यता तत्व "बार" tappable नहीं है। इसे अन्य विचारों से अवरुद्ध किया जा सकता है। 'ध्यान दें कि मैंने यह नहीं लिखा है, लेकिन मैं इसे केआईएफ के साथ स्वचालित करने और फ्लाई पर सीखने की कोशिश कर रहा हूं। – jjj

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