我尝试将 UINavigationBar 添加到我的 UIView。当我只添加 UINavigationBar 时,一切正常。当我用标题和两个按钮定义 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];