gpt4 book ai didi

ios - iPhone UINavigationBar - 设置 rightBarButtonItem 的宽度和边距

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:29 24 4
gpt4 key购买 nike

我有一个按钮的背景图片,尺寸为 80x30 像素。

我使用下面的代码在我的 View Controller 中设置背景,结果是这样的:

Top and corners are noisy

如您所见,右按钮的角和顶部乱七八糟。

- (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];
}

请注意,我在单个 View Controller 中执行此操作,我使用外观 API 来设置一般样式。但在这里我想覆盖一般外观。

如果我只得到没有可调整大小的东西的图像,它看起来像这样:

Too large

现在棱角都好了,就是按钮太大了。

我确定我做错了,所以我需要有人指出我可以做些什么来正确缩放按钮?

最佳答案

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];

关于ios - iPhone UINavigationBar - 设置 rightBarButtonItem 的宽度和边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10314478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com