导航控制器中的后退按钮
这是一个非常基本的问题 - 但我在任何地方都找不到答案。 我想要一个像导航栏中显示的默认按钮一样的后退按钮,但背景中有一个图像。 即使进行了自定义,如何根据标题计算按钮的大小/长度? 非常感谢您的帮助,提前!
更新:
谢谢大家!但我最终实现的答案是这样的:
@implementation UINavigationBar (UINavigationBarCategory)
-(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];
// 使用自定义颜色作为后退按钮,这是我在导航栏图像上使用数字色度计得到的: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)];
}//对我来说效果令人满意
@end
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)
-(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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UIImage
创建一个UIButton
来模仿您想要的形状。UIImage
必须是可拉伸类型,也就是说,您可以将leftCapWidth
设置为后退箭头的大小UIButton
的标题为您喜欢的任何内容UIBarButtonItem
navigationItem
的leftBarButtonItem
属性。该按钮将自动调整大小以适合您的标题。
UIButton
using your ownUIImage
to mimic the shape you want.UIImage
must be a stretchable type, that is, you would set theleftCapWidth
to be the size of your back arrowUIButton
's title to whatever you likeUIBarButtonItem
using your new button as a custom viewnavigationItem
sleftBarButtonItem
property.The button will automatically size to fit your title.
使用
sizeWithFont:
NSString 方法来计算标题的大小。查看 NSString UIKit 添加参考
Use
sizeWithFont:
NSString method to calculate the size of the title.See NSString UIKit Additions Reference
要找出特定文本的宽度,您可以使用以下方法。
我来解释一下上面的两个说法。
在这里,sizeOfBack.width 将为我提供 14System 大小的字符串获取的宽度。
希望对您有帮助。如果您对此还有疑问,请通过评论告诉我。
To find out the width of specific text, you may use following methods.
Let me explain above two statements.
Here, sizeOfBack.width will give me the width acquired by 14System sized string.
Hope it helps to you. let me know by comments, if you have yet doubts regarding this.