当前位置:  开发笔记 > IOS > 正文

具有普通样式的UINavigationBar BarButtonItem

如何解决《具有普通样式的UINavigationBarBarButtonItem》经验,为你挑选了3个好方法。

我得到以下代码:

- (id)init {
  if (self = [super init]) {
      self.title = @"please wait";
      UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"star.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonFavoriteClicked:)];
      self.navigationItem.rightBarButtonItem = favorite;
  }

  return self;
}

但我的按钮看起来仍像一个带有UIBarButtonItemStyleBordered替换文字的按钮 http://img693.imageshack.us/img693/1632/bildschirmfoto20100227ui.png

有没有办法在这个位置设置一个朴素的按钮?



1> 小智..:

试试这个:

- (id)init {
  if (self = [super init]) {
      self.title = @"please wait";

      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
      [button setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
      [button addTarget:self action:@selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside];
      UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithCustomView:button];
      [button release];

      self.navigationItem.rightBarButtonItem = favorite;
  }

  return self;
}

希望能帮助到你.



2> Simon Woodsi..:

在界面构建器中创建一个UIButton,使其看起来完全符合您的要求.在.h中创建一个插座:

IBOutlet UIButton * _myButton;

然后使用自定义视图将其设置为右侧栏按钮项,这将消除边框:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];

这是有效的,因为UIButton是UIView的子类.


此外,如果你想得到阴影效果普通按钮这样做:`_myButton.layer.shadowOpacity = 1.0;``_myButton.layer.shadowRadius = 0.0;``_myButton.layer.shadowColor = [UIColor blackColor] .CGColor;` `_myButton.layer.shadowOffset = CGSizeMake(0.0,[UIScreen mainScreen] .scale == 1?-1:-1.5);`
我同意这个解决方案.但是,你认为苹果在他们的iPad邮件应用中做到了吗?看起来有点小黑客.

3> 小智..:

或者在没有IB的代码中执行此操作:

// add setting button to nav bar
UIImage* settingsGearImg = [UIImage imageNamed:@"settings_gear.png"];
CGRect frameimg = CGRectMake(0, 0, settingsGearImg.size.width, settingsGearImg.size.height);
UIButton *settingsButton = [[UIButton alloc] initWithFrame:frameimg];
[settingsButton setBackgroundImage:settingsGearImg forState:UIControlStateNormal];
[settingsButton addTarget:self action:@selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *settingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:settingsButton];
self.navigationItem.leftBarButtonItem = settingButtonItem;

结果是:

在此输入图像描述

使用此图标图像时(白色背景上为白色,因此在此处不可见 - 链接为:http://i.stack.imgur.com/lk5SB.png): 在此输入图像描述

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有