0

我尝试将 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];
4

2 回答 2

0

你有什么理由不使用navigationItem你的 VC 的财产吗?您不必明确地创建一个新的 UINavigationItem,您可以通过执行以下操作简单地添加您的栏按钮项:

[self.navigationItem setRightBarButtonItem:YourButton];
于 2013-11-05T23:08:09.047 回答
0

当您使用“init”方法时出现了问题。如果您更改为 initWithFrame 一切正常:

UINavigationBar *navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 60)];
于 2013-11-05T22:07:31.193 回答