我尝试将UINavigationBar添加到我的UIView中。当我只添加UINavigationBar时一切正常。当我使用title和两个按钮定义UINavigationItem并将其添加到我的UINavigationBar时出现问题。检查我的代码。
UINavigationBar *navigationBar = [[UINavigationBar alloc]init];
[navigationBar setTintColor:[UIColor whiteColor]];
[navigationBar setBarTintColor:[UIColor redColor]];
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:(20.0)];
NSDictionary *attrNormal = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, font, NSFontAttributeName, nil];
[navigationBar setTitleTextAttributes:attrNormal];
UINavigationItem *navigationItem = [[UINavigationItem alloc]initWithTitle:@"Title"];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:primaryViewController action:nil];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(tableEdit)];
[navigationItem setRightBarButtonItem:addButton animated:YES];
[navigationItem setLeftBarButtonItem:editButton animated:YES];
[self.navigationBar setItems:[NSArray arrayWithObject:navigationItem] animated:YES];
[navigationBar setFrame:CGRectMake(0, 0, self.bounds.size.width, 60)];
[self addSubview:navigationBar];
答案 0 :(得分:0)
当您使用“init”方法时,会发生错误。 如果您更改为initWithFrame,一切正常:
UINavigationBar *navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 60)];
答案 1 :(得分:0)
您是否有任何理由不使用VC的navigationItem
属性?您不必显式创建新的UINavigationItem,只需添加条形按钮项即可:
[self.navigationItem setRightBarButtonItem:YourButton];