我尝试使用 UINavigationItem setLeftBarButtonItem 和 setRightBarButtonItem
以编程方式创建标准导航栏我想拥有看起来像
的NavigationItem[ < BACKTEXT FWDTEXT > ]
我希望在我的图像(箭头)和设备边缘之间保持恒定的空间。 我还想在图像和文本之间保持一个恒定的空间。
我在 UIBarButtonItem 上创建了一个类别,并添加了两个方法 backButtonWith 和 fwdButtonWith
我读了@Split useful answer,解释了如何解决右键
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);
但是我很难解决后退按钮。
这是我的代码
- (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;
}
答案 0 :(得分:0)
您设置为UINavigationItem的后退按钮将显示在下一个导航项上,该导航项将被推送到导航栏。您不会看到当前导航项目。另外,我记得,后退按钮不能包含自定义视图,它应该是普通的UIBarButtonItem。
您可以使用
自定义后退按钮的外观[UIBarButtonItem appearanceWhenContainedIn:<YourNavigationBar.class>]
或具有一般外观
[UIBarButtonItem appearance]
有关后退按钮项目外观自定义的详细信息,请查看此question