Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UINavigationBar中的uiBarButtonim自定义视图_Ios_Objective C_Uinavigationbar_Uibarbuttonitem - Fatal编程技术网

Ios UINavigationBar中的uiBarButtonim自定义视图

Ios UINavigationBar中的uiBarButtonim自定义视图,ios,objective-c,uinavigationbar,uibarbuttonitem,Ios,Objective C,Uinavigationbar,Uibarbuttonitem,我正在为uinavigationbar制作一个自定义的工具栏按钮,我正在使用以下代码 UIImageView *backImgView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chk_back.png"]]; [backImgView setUserInteractionEnabled:YES]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWi

我正在为uinavigationbar制作一个自定义的工具栏按钮,我正在使用以下代码

 UIImageView *backImgView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chk_back.png"]]; [backImgView setUserInteractionEnabled:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backImgView];
[backButton setTarget:self];
[backButton setAction:@selector(BackBtn)];
checkIn_NavBar.topItem.leftBarButtonItem = backButton;

问题是它没有采取行动。我做错了什么?

我使用以下代码完成了此操作:

UIButton *nxtBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
[nxtBtn setImage:[UIImage imageNamed:@"chk_next.png"] forState:UIControlStateNormal];
[nxtBtn addTarget:self action:@selector(NextBtn) forControlEvents:UIControlEventTouchUpInside];
[nxtBtn setFrame:CGRectMake(0, 0, 64, 31)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithCustomView:nxtBtn];

checkIn_NavBar.topItem.rightBarButtonItem=nextButton;

来自苹果文档:
使用指定的自定义视图初始化新项

- (id)initWithCustomView:(UIView *)customView
参数 自定义视图 表示项目的自定义视图。 返回值 具有指定属性的新初始化项

讨论: 此方法创建的条形按钮项不会调用其目标的action方法来响应用户交互。相反,bar按钮项目期望指定的自定义视图处理任何用户交互并提供适当的响应。

解决方案:“使用背景图像创建按钮(将操作设置为此按钮)和使用此按钮创建初始栏按钮”。例如:

UIButton *btn =  [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,25,25)
[btn setBackgroundImage:[UIImage imageNamed:@"chk_back.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(BackBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];

我能看看你的BackBtn方法吗?什么是签入导航栏?你对它有强烈的引用吗?为什么不使用self.navigationItem.LeftBarButtonim?尝试使用[checkIn_NavBar SetLeftBarButtonim:backButton];或self.navigationItem.LeftBarButtonim=backButton;checkIn_导航栏有一个强引用,并且没有调用BackBtn方法。因此,您可以在按钮上添加一个按钮。听起来一点也不对劲。@Popeye
UIBarButtonItem
不是按钮,看看它是继承的。@BryanBryce该死的,这是旧的,我想我更多的是说,
UIBarButtonItem
本身就是按钮的一种形式,而不一定是说它继承自
UIButton
——谢谢你测试我的记忆。回答很好,但是:docu说:“当创建一个自定义按钮,它是一个UIButtonTypeCustom类型的按钮时,按钮的框架设置为(0,0,0,0)初始值。在将按钮添加到界面之前,应将帧更新为更合适的值。“所以你应该确定设置按钮的框架,否则你什么也看不到v@ReinhardMänner-你救了我的lfe,伙计!而且,这是一个可怕的帧默认值…@katzenhut-很高兴我能帮助你,你对默认值完全正确…我添加了帧的规格,并使其为75x44,即similar设置为默认的UIBarButtonItem。使图像为25x25可视区域,周围为透明度。