2013-08-27 11 views
11

Próbuję programowo dodać UINavigationBar do UIView i mając taki sukces, i nie mogę go rozwiązać. Próbuję dodać go do mojej podklasy UIView i po prostu nie wyświetla się po uruchomieniu aplikacji.Dodawanie UINavigationBar do UIView programowo?

Oto mój kod:

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

W jakiej metody masz ten kod? Po prostu spróbowałem i działa to dla mnie. Ponadto ustawienie backgroundColor nie będzie miało żadnego efektu.Trzeba użyć właściwości "tintColor". – LuisCien

+0

Został wywołany podczas metody init wewnątrz instrukcji 'if (self)'. Nie rozumiem, dlaczego to by przestało działać, ponieważ dodam tutaj inne subviews. –

+0

Cóż, to bardzo dziwne, ponieważ po prostu próbowałem powtórzyć twój scenariusz i to działa dla mnie. Wydaje mi się, że problem może polegać na tym, że "headerHeight" jest ustawione na 0. – LuisCien

Odpowiedz

7

Jeśli używasz kontrolera nawigacyjnego od pierwszego widzenia Proponuję to w Appdelegate.m

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

ale jeśli masz widok prezentujący Twój nowy widok, który ma mieć pasek nawigacji, służy do wyświetlania nowego widoku:

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

następnie możesz dodać swoje navBarButtons w drugim widoku, jak poniżej:

- (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

Dzięki za sugestię, ale wyjaśniłem w pytaniu. Próbuję dodać UINavigationBar do UIView. Żadni kontrolerzy nie są częścią tego problemu. –

5

Nie jestem pewien, co się dzieje. Oto kod, który używam do odtworzenia problemu:

Gwiazdka * .h plik jest pusty

#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 

I to jest to, co ja dostaję:

enter image description here

Czy możesz:

  1. Dodać kod dla całej metody initWithFrame:?
  2. Upewnij się, że self.frame i headerHeight są właściwie ustawione poprawnie przed uruchomieniem kodu?
  3. Pamiętaj, aby nie używać właściwości backgroundColor ale właściwość

nadzieję, że to pomaga barTintColor!

3

Nie mogę ci powiedzieć dlaczego, ale doświadczyłem tego samego problemu i wyśledziłem go metodą setBackgroundColor. Aby obejść to zrobiłem:

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

Może zapomniałeś "navBar.items = @ [navItem];" jak miałem do czynienia z podobnym problemie:

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]; 

użyć tych właściwości: „barTintColor”, aby uzyskać kolor paska nawigacyjnego tle i „tintColor” dla elementów nawigacyjnych i przedmiotów przycisk paska.

0

Jeśli używasz XIb zamiast ujęć, a następnie napisać te wiersze w AppDelegate:

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

Jeśli używasz storyboard to jego dobrą dodać NavigationController z menu.