25

मेरे आवेदन में, मैं नेविगेशन बार शीर्षक और उपशीर्षक प्रदर्शित करना चाहता हूं।नेविगेशन बार में आईफोन शीर्षक और उपशीर्षक

कि हद तक, मैं मेरे विचार नियंत्रक के लिए निम्न कोड कहा:

// Replace titleView 
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);  
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease]; 
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor]; 
_headerTitleSubtitleView.autoresizesSubviews = YES; 

CGRect titleFrame = CGRectMake(0, 2, 200, 24); 
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease]; 
titleView.backgroundColor = [UIColor clearColor]; 
titleView.font = [UIFont boldSystemFontOfSize:20]; 
titleView.textAlignment = UITextAlignmentCenter; 
titleView.textColor = [UIColor whiteColor]; 
titleView.shadowColor = [UIColor darkGrayColor]; 
titleView.shadowOffset = CGSizeMake(0, -1); 
titleView.text = @""; 
titleView.adjustsFontSizeToFitWidth = YES; 
[_headerTitleSubtitleView addSubview:titleView]; 

CGRect subtitleFrame = CGRectMake(0, 24, 200, 44-24); 
UILabel *subtitleView = [[[UILabel alloc] initWithFrame:subtitleFrame] autorelease]; 
subtitleView.backgroundColor = [UIColor clearColor]; 
subtitleView.font = [UIFont boldSystemFontOfSize:13]; 
subtitleView.textAlignment = UITextAlignmentCenter; 
subtitleView.textColor = [UIColor whiteColor]; 
subtitleView.shadowColor = [UIColor darkGrayColor]; 
subtitleView.shadowOffset = CGSizeMake(0, -1); 
subtitleView.text = @""; 
subtitleView.adjustsFontSizeToFitWidth = YES; 
[_headerTitleSubtitleView addSubview:subtitleView]; 

_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
             UIViewAutoresizingFlexibleRightMargin | 
             UIViewAutoresizingFlexibleTopMargin | 
             UIViewAutoresizingFlexibleBottomMargin); 

self.navigationItem.titleView = _headerTitleSubtitleView; 

और यह भी एक विधि लागू:

-(void) setHeaderTitle:(NSString*)headerTitle andSubtitle:(NSString*)headerSubtitle { 
    assert(self.navigationItem.titleView != nil); 
    UIView* headerTitleSubtitleView = self.navigationItem.titleView; 
    UILabel* titleView = [headerTitleSubtitleView.subviews objectAtIndex:0]; 
    UILabel* subtitleView = [headerTitleSubtitleView.subviews objectAtIndex:1]; 
    assert((titleView != nil) && (subtitleView != nil) && ([titleView isKindOfClass:[UILabel class]]) && ([subtitleView isKindOfClass:[UILabel class]])); 
    titleView.text = headerTitle; 
    subtitleView.text = headerSubtitle; 
} 

चीजें, खूबसूरती से काम धन्यवाद।

सिवाय इसके कि आईफोन को लैंडस्केप में घुमाने पर, शीर्षक + उपशीर्षक नेविगेशन आइटम के डिफ़ॉल्ट शीर्षक की तरह स्वचालित तरीके से डाउनसाइज नहीं किया जाता है।

कोई बात नहीं?

धन्यवाद!

+1

आपका कोड अच्छी तरह से काम करता है और अच्छी तरह से लिखा है का उपयोग करना चाहिए, पोस्ट करने का शुक्रिया। जब आप setHeaderTitle को कॉल कर रहे हैं? मैंने इसे करने का प्रयास किया है जब धक्का दृश्य नियंत्रक दृश्य विल्लएपियर को कॉल करता है लेकिन फिर यह बहुत जल्दी स्विच करता है औरडिडपियर बहुत देर से स्विच करता है। –

+0

मैं इसे init विधियों में बुलाता हूं। – Reuven

उत्तर

7

titleView और subtitleView लेबल नियंत्रक के गुणों में लेबल बनाएं, ताकि आप उन्हें किसी भी विधि से एक्सेस कर सकें। फिर, दृश्य नियंत्रक के रोटेशन पर, लेबल का आकार बदलने:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [self adjustLabelsForOrientation:toInterfaceOrientation]; 
} 

- (void) adjustLabelsForOrientation:(UIInterfaceOrientation)orientation { 
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { 
     titleView.font = [UIFont boldSystemFontOfSize:16]; 
     subtitleView.font = [UIFont boldSystemFontOfSize:11]; 
    } 
    else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     titleView.font = [UIFont boldSystemFontOfSize:20]; 
     subtitleView.font = [UIFont boldSystemFontOfSize:13]; 
    } 
} 
+0

धन्यवाद - यह काम करता है। (मुझे अभी भी लैंडस्केप फ़ॉन्ट आकार के साथ खेलने की जरूरत है, लेकिन दिशा काम करता है) – Reuven

1

इसके बजाय इस authoresizing का मुखौटा

_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
             UIViewAutoresizingFlexibleRightMargin | 
             UIViewAutoresizingFlexibleTopMargin | 
             UIViewAutoresizingFlexibleBottomMargin); 

आप

_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
             UIViewAutoresizingFlexibleHeight; 

titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
              UIViewAutoresizingFlexibleHeight; 
subtitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
              UIViewAutoresizingFlexibleHeight; 
संबंधित मुद्दे