通常当你使用UINavigationController
时,你会得到一个后面的按钮,它的左侧有一个箭头。问题是我没有使用UINavigationController,这就是我要问的原因。无论如何这是一张图片:
到目前为止,我有一个常规的UIBarButtonItem
,目前只是正方形而没有左侧的箭头。
有没有办法在左侧制作一个箭头,并将其添加到我的UINavigationBar?我也环顾四周,似乎UIButton 101型没有记录,会引起拒绝。我需要一个可以接受的解决方案!
答案 0 :(得分:3)
您可以为按钮指定一个背景图像,使其看起来像原生角度按钮:
testButton = [[UIButton alloc] initWithFrame:CGRectMake(80, 30, 160, 44)];
[testButton setTitle:@"Test Button" forState:UIControlStateNormal];
UIImage *buttonImage = [[UIImage imageNamed:@"angledButton"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
[testButton addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[testButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
你可能需要调整一下,但我认为这是正确的方向。
答案 1 :(得分:2)
顺便说一下。如果你想创建一个与导航控制器中的后退按钮完全相同的按钮,请查看此代码..
@implementation UIButton (CustomBackButton)
- (UIButton*)configureForBackButtonWithTitle:(NSString*)title target:(id)target action:(SEL)action
{
// Make the text 6 pixels from above, 8 pixels from the right, and 12 pixels from the left of button's frame.
CGFloat padTRL[3] = {6, 8, 12};
// Text must be put in its own UIView, s.t. it can be positioned to mimic system buttons
UILabel* label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:12];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0, -1);
label.text = title;
[label sizeToFit];
UIImage* norm = [[UIImage imageNamed:@"backBarButton.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
UIImage* click = [[UIImage imageNamed:@"backBarButtonHover.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
[self setBackgroundImage:norm forState:UIControlStateNormal];
[self setBackgroundImage:click forState:UIControlStateHighlighted];
[self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
// Calculate dimensionss
CGSize labelSize = label.frame.size;
CGFloat controlWidth = labelSize.width+padTRL[1]+padTRL[2];
controlWidth = controlWidth>=norm.size.width?controlWidth:norm.size.width;
// Assemble and size the views
self.frame = CGRectMake(0, 0, controlWidth, 30);
[self addSubview:label];
label.frame = CGRectMake(padTRL[2], padTRL[0], labelSize.width, labelSize.height);
return self
}
@end
并且您不需要使用整个图像。只是尖尖的部分会做..它根据标题的长度拉伸图像..
答案 2 :(得分:1)
首先,您必须找到后退按钮的图像。我使用了一个名为Extractor的漂亮应用程序,它可以从iPhone中提取所有图形。
在 iOS7 中,我设法检索名为UINavigationBarBackIndicatorDefault
的图像并且它是黑色的,因为我需要红色,我使用Gimp将颜色更改为红色。
然后我创建了一个视图,其中包含带有该箭头的imageView,带有自定义文本的标签,并且在视图顶部,我有一个带动作的按钮。然后我添加了一个简单的动画(淡入淡出和翻译)。
以下代码模拟后退按钮的行为,包括动画。
-(void)viewWillAppear:(BOOL)animated{
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBarBackIndicatorDefault"]];
[imageView setTintColor:[UIColor redColor]];
UILabel *label=[[UILabel alloc] init];
[label setTextColor:[UIColor redColor]];
[label setText:@"Blog"];
[label sizeToFit];
int space=6;
label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space, imageView.frame.size.height)];
view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width, view.bounds.size.height);
[view addSubview:imageView];
[view addSubview:label];
UIButton *button=[[UIButton alloc] initWithFrame:view.frame];
[button addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
[UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
label.alpha = 0.0;
CGRect orig=label.frame;
label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
label.alpha = 1.0;
label.frame=orig;
} completion:nil];
UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:view];
}
- (void) handleBack:(id)sender{
}