2013-08-27 5 views
11

मैं प्रोग्रामिंग के लिए एक UIView में UINavigationBar जोड़ने और इतनी सफलता प्राप्त करने की कोशिश कर रहा हूं, और इसे समझ नहीं सकता। मैं इसे अपने UIView सबक्लास में जोड़ने की कोशिश कर रहा हूं और जब मैं अपना ऐप चलाता हूं तो यह बस दिखाई नहीं दे रहा है।UIViewigationBar को UIView को प्रोग्रामिक रूप से जोड़ना?

यहाँ मेरी कोड है:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, headerHeight)]; 
    [navBar setBackgroundColor:[UIColor blueColor]]; 
    [self addSubview:navBar]; 

    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)]; 

    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"categories"]; 
    [navItem setRightBarButtonItem:doneItem animated:YES]; 
    [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES]; 
+0

आपके पास यह कोड किस विधि में है? मैंने अभी कोशिश की और यह मेरे लिए काम कर रहा है। साथ ही, पृष्ठभूमि रंग सेट करने से कोई प्रभाव नहीं पड़ेगा।आपको "tintColor" संपत्ति – LuisCien

+0

का उपयोग करना होगा, इसे 'if (self)' कथन के अंदर init विधि के दौरान बुलाया जाता है। मैं नहीं देखता कि यह काम क्यों बंद कर देगा, क्योंकि मैं यहां अन्य सबव्यू जोड़ता हूं। –

+0

ठीक है, यह वास्तव में बहुत अजीब है क्योंकि मैंने अभी आपके परिदृश्य को पुन: पेश करने की कोशिश की है और यह मेरे लिए काम कर रहा है। मेरा मानना ​​है कि समस्या यह हो सकती है कि 'हेडरहेइट' 0 पर सेट है – LuisCien

उत्तर

7

आप पहली बार देखने मैं अपने AppDelegate.m

UIViewController *viewController = [[MenuViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
     self.window.rootViewController=navController; 

में यह सुझाव है लेकिन से नेविगेशन नियंत्रक का उपयोग कर रहे हैं यदि आप कोई दृश्य जो प्रस्तुत करता है, तो आपका नया दृश्य जो आप चाहते हैं कि नया दृश्य दिखाने के लिए एक Navbar इसका उपयोग करें:

UIViewController *viewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil]; 
      UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
[self presentViewController:navigationcontroller animated:YES completion:nil]; 

फिर आप अपने NavBarButtons को दूसरे दृश्य में नीचे जोड़ सकते हैं:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 

     UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(btnAddGroupPressed)]; 
     self.navigationItem.rightBarButtonItem=btnAdd; 

    } 
    return self; 
} 
+0

आपके सुझाव के लिए धन्यवाद लेकिन मैंने इस प्रश्न में समझाया है। मैं UIView में एक UINavigationBar जोड़ने की कोशिश कर रहा हूं। कोई भी नियंत्रक इस मुद्दे का हिस्सा नहीं हैं। –

5

मुझे यकीन नहीं है कि क्या हो रहा है। यहाँ कोड है कि मैं आपकी समस्या को पुनः करने के लिए उपयोग कर रहा हूँ है:

* ज फ़ाइल खाली है

#import "STTestView.h" 

@implementation STTestView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

     UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 300, 50)]; 
     navBar.barTintColor = [UIColor purpleColor]; 
     [self addSubview:navBar]; 

     UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)]; 

     UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"categories"]; 
     [navItem setRightBarButtonItem:doneItem animated:YES]; 
     [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES]; 

     self.backgroundColor = [UIColor darkGrayColor]; 
    } 
    return self; 
} 

-(void)done:(id)sender { 

} 

@end 

और यह मैं क्या हो रही है है:

enter image description here

क्या आप कृपया:

  1. पूरे initWithFrame: विधि के लिए कोड जोड़ें?
  2. सुनिश्चित करें कि आपका कोड चलाने के समय से self.frame और headerHeight वास्तव में सही ढंग से सेट हो गए हैं?
  3. backgroundColor संपत्ति लेकिन barTintColor संपत्ति

आशा इस मदद करता है का उपयोग नहीं याद रखें!

3

मैं आपको नहीं बता सकता कि क्यों, लेकिन मैंने इस समस्या का अनुभव किया है और मैंने इसे setBackgroundColor विधि पर देखा है। इसके चारों ओर पाने के लिए मैंने किया:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44.0)]; 
UINavigationItem *titleItem = [[UINavigationItem alloc] initWithTitle:@"MyNavBar"]; 
NSDictionary *titleAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
              [UIColor whiteColor], 
              UITextAttributeTextColor, 
              [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0], 
              UITextAttributeTextShadowColor, 
              [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
              UITextAttributeTextShadowOffset, 
              [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], 
              UITextAttributeFont, 
              nil]; 
navBar.titleTextAttributes = titleAttributesDictionary; 
navBar.items = @[titleItem]; 
navBar.tintColor=[UIColor blueColor]; 
[self addSubview:navBar]; 
1

शायद, आप भूल गया "navBar.items = @ [navItem]," के रूप में मैं इसी तरह के मुद्दे से निपटा:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, headerHeight)]; 
    navBar.barTintColor = [UIColor blueColor]; 
    [self addSubview:navBar];//it is important point, otherwise, you can see only the area without any item 

    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)]; 

    [navItem setRightBarButtonItem:doneItem animated:YES]; 
    [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES]; 

उपयोग इन गुणों: "barTintColor" नेविगेशन पट्टी पृष्ठभूमि का रंग, और "tintColor" नेविगेशन वस्तुओं और बार बटन मदों के लिए मिलता है।

0

आप स्टोरीबोर्ड के बजाय xib का उपयोग कर रहे हैं, तो AppDelegate में इन पंक्तियों के बारे में:

UIViewController *viewControllerObj = [[MenuViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewControllerObj]; 
self.window.rootViewController=navController; 

आप मेनू से navigationController जोड़ने के लिए स्टोरीबोर्ड तो इसके अच्छे उपयोग कर रहे हैं।

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