iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – UIButton作为UINavigationbar按钮大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我有一个Tab栏应用程序.在其中一个标签栏中,我有一个导航控制器. 我试图设置一个UIButton与图像作为导航栏的右键. UIButton *refreshButton = [[UIButton alloc] init]; [refreshButton setImage:[UIImage imagenamed:@"refresh_icon.png"] forState:UIControl
@H_197_18@ 我有一个tab栏应用程序.在其中一个@L_@R_696_11235@_4@栏中,我有一个导航控制器.
我试图设置一个UIButton与图像作为导航栏的右键.

UIButton *refreshButton = [[UIButton alloc] init];
    [refreshButton setImage:[UIImage imagenamed:@"refresh_icon.png"] forState:UIControlStateNormal];
    [refreshButton addTarget:self action:@SELEctor(refreshSection) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
    //[rightButton setImage:[UIImage imagenamed:@"refresh_icon.png"]]; << DOESN'T WORK EITHER  
    self.navigationItem.rightBarButtonItem = rightButton;
    [refreshButton release];
    [rightButton release];

但图像没有显示出来.我得到一个隐形按钮.

编辑:
我希望按钮样​​式为普通或背景为透明,以便显示图像.因此我使用UI按钮而不是直接使用UIBarButton.

解决方法

您不必创建UIButton并将其用作UIBarButtonItem中的自定义视图.您可以直接使用图像创建UIBarButtonItem:

UIImage *myImage = [UIImage imagenamed:@"myImage.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:@SELEctor(refreshSection)];  
self.navigationItem.rightBarButtonItem = button;    
[button release];

根据评论更新

在UIBarButtonItem中使用UIImageView作为自定义视图:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imagenamed:@"myImage.png"]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];

更新

尝试了最后一种方法,然这看起来像你想要它,我似乎无法让它来处理水龙头.然目标和行动已经确定.

最后更新

我已经在问题中获得了您的初始代码&运行.图像未显示的原因是因为您尚未设置按钮的框架.设置框架后,图像将显示在导航栏中.

这是我使用过的片段&测试:

UIImage *myImage = [UIImage imagenamed:@"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0,0.0,myImage.size.width,myImage.size.height);

[myButton addTarget:self action:@SELEctor(tapped:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;

[rightButton release];
[myButton release];

注意:我已经设置了UIButton的showsTouchWhenHighlighted属性,以便在按下按钮时可视地高亮显示该按钮.

大佬总结

以上是大佬教程为你收集整理的iphone – UIButton作为UINavigationbar按钮全部内容,希望文章能够帮你解决iphone – UIButton作为UINavigationbar按钮所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。