[英]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. 让我解释以上两个陈述。
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.
如果您对此有疑问,请通过评论让我知道。
UIButton
using your own UIImage
to mimic the shape you want. UIImage
创建一个UIButton
来模仿您想要的形状。 UIImage
must be a stretchable type, that is, you would set the leftCapWidth
to be the size of your back arrow UIImage
必须是可拉伸的类型,也就是说,您可以将leftCapWidth
设置为后退箭头的大小 UIButton
's title to whatever you like UIButton
的标题设置为您喜欢的任何内容 UIBarButtonItem
using your new button as a custom view UIBarButtonItem
navigationItem
s leftBarButtonItem
property. navigationItem
的leftBarButtonItem
属性。 The button will automatically size to fit your title. 该按钮将自动调整大小以适合您的标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.