简体   繁体   English

导航控制器中的“后退”按钮

[英]Back Button in navigation controller

Its a very basic question - but i could not find the answer to it anywhere. 这是一个非常基本的问题-但我在任何地方都找不到答案。 I would like a back button like the default one that shows up in a navigation bar, but with a image in the background. 我希望后退按钮像默认按钮一样显示在导航栏中,但背景中有图像。 Even with customization, how to calculate the size/length of the button as per the title? 即使进行自定义,如何根据标题计算按钮的大小/长度? Thanks a ton for the help,in advance! 提前感谢您的帮助!

UPDATE: 更新:

Thanks guys! 多谢你们! but the answer that i finally implemented was this: 但是我最终实现的答案是这样的:

@implementation UINavigationBar (UINavigationBarCategory) @implementation UINavigationBar(UINavigationBarCategory)

-(void)drawRect:(CGRect)rect -(void)drawRect:(CGRect)rect

{ {

UIColor *color = [UIColor colorWithRed:(13.0/255.0) green:(183.0/255.0) blue:(255.0/255.0) alpha:1.0];

// use a custom color for the back button which i got using the digital color meter on my nav bar image :P //使用自定义颜色作为后退按钮,这是我在导航栏图像上使用数字色度计获得的:P

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));

CGContextFillRect(context, rect);
self.tintColor = color;
// use a custom background image for my navigation bar

UIImage *img = [UIImage imageNamed: Navigation_img];

[img drawInRect:CGRectMake(0,0,img.size.width,img.size.height)];

}//worked satisfactorily for me } //对我来说令人满意

@end @结束

Use sizeWithFont: NSString method to calculate the size of the title. 使用sizeWithFont: NSString方法来计算标题的大小。

See NSString UIKit Additions Reference 请参见NSString UIKit附加参考

To find out the width of specific text, you may use following methods. 要找出特定文本的宽度,可以使用以下方法。

 NSString *str=@"Back";

CGSize sizeOfBack = [str sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(30, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];

Let me explain above two statements. 让我解释以上两个陈述。

  1. the text that you want to have in your string. 您想要在字符串中包含的文本。
  2. sizewithfont method will allow you to calculate the size. sizewithfont方法将允许您计算大小。

Here, sizeOfBack.width will give me the width acquired by 14System sized string. 在这里,sizeOfBack.width将给我14System大小的字符串获得的宽度。

Hope it helps to you. 希望对您有帮助。 let me know by comments, if you have yet doubts regarding this. 如果您对此有疑问,请通过评论让我知道。

  • Create a UIButton using your own UIImage to mimic the shape you want. 使用您自己的UIImage创建一个UIButton来模仿您想要的形状。
  • The UIImage must be a stretchable type, that is, you would set the leftCapWidth to be the size of your back arrow UIImage必须是可拉伸的类型,也就是说,您可以将leftCapWidth设置为后退箭头的大小
  • Set the UIButton 's title to whatever you like UIButton的标题设置为您喜欢的任何内容
  • Create a new UIBarButtonItem using your new button as a custom view 使用新按钮作为自定义视图创建新的UIBarButtonItem
  • Set this to your navigationItem s leftBarButtonItem property. 将此设置为navigationItemleftBarButtonItem属性。

The button will automatically size to fit your title. 该按钮将自动调整大小以适合您的标题。

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

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