Ios iPhone UINavigationBar-设置右按钮的宽度和边距
我有一个按钮的背景图像,尺寸为80x30像素 我使用下面的代码在我的视图控制器中设置背景,结果如下: 正如你所看到的,右按钮的角落和顶部乱七八糟Ios iPhone UINavigationBar-设置右按钮的宽度和边距,ios,ios5,Ios,Ios5,我有一个按钮的背景图像,尺寸为80x30像素 我使用下面的代码在我的视图控制器中设置背景,结果如下: 正如你所看到的,右按钮的角落和顶部乱七八糟 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIImage *favoriteBtnImgNormal = [[UIImage imageNamed:@"favorite-btn-n
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImage *favoriteBtnImgNormal = [[UIImage imageNamed:@"favorite-btn-normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
UIImage *favoriteBtnImgTouch = [[UIImage imageNamed:@"favorite-btn-touch"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgTouch forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgTouch forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(-10.0, 0.0) forBarMetrics:UIBarMetricsDefault];
}
请注意,我在单个视图控制器中执行此操作,我使用外观API设置常规样式。但在这里,我想覆盖一般的外观
如果我只是得到没有可调整大小的图片,它看起来像这样:
现在边和角都很好,但是按钮太大了
我肯定我做错了,所以我需要有人指出我能做些什么来正确缩放按钮?大家好,欢迎来到Stackoverflow!请仔细阅读。答案应该不仅仅是一个代码转储或到另一个站点的链接。答案应该能够独立存在,并包含足够的上下文,以便有意义。我意识到我的问题没有得到回答,尽管我已经找到了一种方法,通过使用外观API实现我想要的,并使用可重新调整大小的图像,这是实现这一点的首选方法。
UIImage *image = [UIImage imageNamed:@"YourImage"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(YourAction) forControlEvents:UIControlEventTouchUpInside];
button.contentEdgeInsets = (UIEdgeInsets){.right=-10};
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];