【问题标题】:Customize NavigationBar in IOS Objective c Application在 IOS Objective c 应用程序中自定义 NavigationBar
【发布时间】:2016-10-27 12:44:46
【问题描述】:

我想要这样的导航栏。

我可以通过以下代码设置左按钮:

-(void) setNavigationButtons:(NSString *)title{
    UIImage *logoImage = [UIImage imageNamed:@"back-icon"];
    UIButton *logoBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [logoBackButton setImage:logoImage forState:UIControlStateNormal];
    logoBackButton.frame = CGRectMake(0.0, 0.0, 50, 50);
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:logoBackButton];
    [logoBackButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchDown];
    [self.navigationController.navigationBar setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[Constants Instance].whiteColor}];
    self.navigationController.navigationBar.barTintColor = [Constants Instance].navigationTitleColor;
    [self setNavigationTitle:title];
    self.navigationItem.leftBarButtonItem = backBarButtonItem;
}

-(void)setNavigationTitle:(NSString *)title{
    self.navigationItem.title = title;
    [self.navigationController.navigationBar setTitleTextAttributes:
    @{NSForegroundColorAttributeName:[UIColor whiteColor],
    NSFontAttributeName:[UIFont fontWithName:@"Nexa Light" size:20]}];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

但它不附加到左侧并且有边距。我希望它像上面的屏幕截图一样固定在左侧。我不想在该按钮上显示任何文本,例如返回等。

右键发生类似情况。

编辑 这是结果:

如果我在代码末尾添加这一行,我会遇到更多问题。

self.navigationItem.rightBarButtonItem = backBarButtonItem;

为了显示右键,它只显示右键而不显示左键。意味着我不能同时设置两个按钮。

【问题讨论】:

  • 请添加实际结果的截图,以便更好地理解问题。
  • 请张贴你所取得的成果,以便其他人可以调整它。
  • 好的,我已经删除了该代码以供其他尝试,请给我 2 分钟,我会上传有问题的。
  • 我添加了截图

标签: ios objective-c uinavigationbar


【解决方案1】:

隐藏真正的导航栏并添加UIView,它将作为navigation bar工作。并在其上添加两个按钮。您的视图框架应为(0,0,64,screenWidth)。这对您来说会更容易,因为根据您的屏幕截图,您不需要此屏幕的后退按钮,因此您应该为此屏幕隐藏 navigation bar,这比自定义真正的导航栏更容易!

【讨论】:

  • 我可以了解更多详情吗?导航控制器不允许我在导航控制器中添加任何内容。
  • 隐藏导航栏,从 viewdidload 添加新的 uiview 以及我在回答中提到的框架,并向该视图添加两个按钮。不是这样!你也可以从 interfacebuilder 做同样的事情!
  • 这意味着我必须在我所有的 VC 中添加它?
  • 类似这样的东西:-(void) viewWillAppear [super viewWillAppear]; CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat screenWidth = screenRect.size.width UIView *myNavView = [UIView alloc] initWithFrame:CGRectMake(0, 0, 64, screenWidth)]; [[self view] addSubview: myNavView]; 然后添加你的按钮。我同意Lion的观点,这是更好的方法。然后,您将能够根据需要制作所有想要的动画。
【解决方案2】:

请移除您的导航栏。显示带有左右按钮的自定义视图

-(void) viewWillAppear {
[super viewWillAppear]; 
self.navigationcontroller.navigationbar.setHidden = YES;
CGRect screenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = screenRect.size.width 
UIView *myNavView = [UIView alloc] initWithFrame:CGRectMake(0, 0, 64, screenWidth)]; 
[[self view] addSubview: myNavView];
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多