简体   繁体   English

在导航栏中添加左右UIBarButtonItem

[英]Adding left and right UIBarButtonItem in navigation bar

I'm trying to create a standard navigation bar programmatically using UINavigationItem setLeftBarButtonItem and setRightBarButtonItem 我正在尝试使用UINavigationItem setLeftBarButtonItemsetRightBarButtonItem以编程方式创建标准导航栏

I want to have NavigationItem that looks like 我想要一个看起来像的NavigationItem

[ < BACKTEXT     FWDTEXT > ]

I want to have a constant space between my images (arrow) and device edges. 我希望图像(箭头)和设备边缘之间保持恒定的空间。 I would also want to have a constant space between images and texts. 我还希望图像和文本之间保持恒定的间距。

I created a category over UIBarButtonItem and added two method backButtonWith and fwdButtonWith 我在UIBarButtonItem上创建了一个类别,并添加了两个方法backButtonWithfwdButtonWith

I read @Split useful answer that explained how to solve the right button 我阅读了@Split 有用的答案 ,该答案解释了如何解决右侧按钮

button.titleEdgeInsets = UIEdgeInsetsMake(0, - button.imageView.frame.size.width, 0, button.imageView.frame.size.width);
button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width, 0, -button.titleLabel.frame.size.width);

But I'm having a hard time resolving the back button. 但是我很难解决后退按钮。

Here is my code 这是我的代码

- (UIBarButtonItem *)buttonWith:(BOOL) directionLeft title:(NSString *)title image:(UIImage *) buttonImage tintColor:(UIColor *)color target:(id)target andAction:(SEL)action{
  UIButton *barButton;
  barButton = [UIButton buttonWithType:UIButtonTypeCustom];
  float buttonWidth = 130.0f;
  [barButton setFrame:CGRectMake(0, 0, buttonWidth, 20.5f)];
  [barButton setTitle:title forState:UIControlStateNormal];
  [barButton setTitleColor:color forState:UIControlStateNormal];
  const CGFloat* components = CGColorGetComponents(color.CGColor);
  [barButton setTitleColor:[UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:0.3f] forState:UIControlStateHighlighted];

  UIEdgeInsets titleEdgeInsets;
  UIEdgeInsets imageEdgeInsets;
  UIImage *image = [self image:buttonImage tintedWithColor:color fraction:0.0];
  [barButton setImage:image forState:UIControlStateNormal];
  [barButton setImage:[self image:image byApplyingAlpha:0.3] forState:UIControlStateHighlighted];

  [barButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  if(directionLeft == YES){
     titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);//THIS PARAMS SHOULD BE CONFIGURED
     imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0);
  }else{
      titleEdgeInsets = UIEdgeInsetsMake(0, -barButton.imageView.frame.size.width, 0, barButton.imageView.frame.size.width);
      imageEdgeInsets = UIEdgeInsetsMake(0, barButton.titleLabel.frame.size.width, 0, -barButton.titleLabel.frame.size.width);
  }
  barButton.imageEdgeInsets = imageEdgeInsets;
  barButton.titleEdgeInsets = titleEdgeInsets;

  UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
  return barButtonItem;
}

The back button which you set to UINavigationItem appears on the next navigation item which is pushed to the navigation bar. 您设置为UINavigationItem的后退按钮将出现在下一个导航项目中,该项目已被推到导航栏。 You won't see in on the current navigation item. 您不会在当前导航项中看到。 Also, as I remember, back button is not able to contain custom view, it should be plain UIBarButtonItem. 另外,我记得,后退按钮不能包含自定义视图,它应该是普通的UIBarButtonItem。

You can customize the appearance of your back button with 您可以使用以下方法自定义后退按钮的外观

[UIBarButtonItem appearanceWhenContainedIn:<YourNavigationBar.class>] 

or with general appearance 或外观一般

[UIBarButtonItem appearance]

For details on back button item appearance customization, please check out this question 有关自定义后退按钮项外观的详细信息,请查看此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM