gpt4 book ai didi

iphone - 使导航 Controller 中的自定义后退按钮可点击区域变小

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

我用下面的代码创建了一个自定义后退按钮,但是可点击区域非常大,远远超出了图标本身。有谁知道如何设置可点击区域,或使其与图片大小相同?

谢谢

UIImage *buttonImage = [UIImage imageNamed:@"prefs"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:buttonImage forState:UIControlStateNormal];

button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

[button addTarget:self action: @selector(handleBackButton)
forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

self.navigationItem.leftBarButtonItem = customBarItem;

可点击区域显示为红色。

Clickable area

谢谢!

最佳答案

如果你想阻止按钮以外的点击,那么将自定义按钮添加到 UIView 然后将该 View 设置为自定义 View 到 barbuttonItem

你的代码会变成这样:

UIImage *buttonImage = [UIImage imageNamed:@"prefs"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action: @selector(handleBackButton)
forControlEvents:UIControlEventTouchUpInside];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[view addSubview:button];

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.leftBarButtonItem = customBarItem;

这对我来说应该有效。

关于iphone - 使导航 Controller 中的自定义后退按钮可点击区域变小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16553844/

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