【问题标题】:UINavigationBar BarButtonItem with Plain StyleUINavigationBar BarButtonItem 与普通样式
【发布时间】:2010-02-27 18:21:34
【问题描述】:

我得到了以下代码:

- (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 的按钮

有没有办法在这个位置设置一个普通样式的按钮?

【问题讨论】:

    标签: iphone image uinavigationbar uibarbuttonitem


    【解决方案1】:

    在界面生成器中创建一个 UIButton,使其看起来完全符合您的要求。在您的 .h 中为 in 创建一个出口:

    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 邮件应用程序中会这样做吗?似乎有点小技巧。
    • @ajayjapan UI 指南说导航栏会忽略项目的 style 属性,因为无边框按钮不适合导航栏。至于邮件,左侧有一个导航栏(有边框按钮),右侧有一个工具栏(允许无边框按钮)。
    【解决方案2】:

    试试这个:

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

    希望对你有帮助。

    【讨论】:

    • 分配后你也应该释放favorite
    【解决方案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):

    【讨论】:

      【解决方案4】:

      这很奇怪,但它有效。对图片/网点说“不”!您甚至不应该设置 UIBarButtonItemStylePlain 属性。诀窍是将按钮放入具有一些特殊属性的 UIToolBar:

      UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Cool plain bar"];
      UIToolbar *tools = [[UIToolbar alloc]
                          initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.01f)];
      tools.clipsToBounds = NO;
      tools.barStyle = -1; // clear background
      NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
      UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshTableView)];
      [buttons addObject:bi];
      [tools setItems:buttons animated:NO];
      UIBarButtonItem *rightButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
      
      item.rightBarButtonItem = rightButtons;
      item.hidesBackButton = YES;
      [myCoolNavigationBar pushNavigationItem:item animated:NO];
      

      【讨论】:

        【解决方案5】:

        虽然 Frank 是对的,但这段代码应该在 viewDidLoad 中,这里的问题是 BarButtons 的样子。如果你想让它看起来不一样,你需要使用不同类型的 UIView。您可以将 UIButton 添加到 UIToolbar 就好了。

        -(void)viewDidLoad {
          [super viewDidLoad];
          self.title = @"please wait";
          self.navigationItem.rightBarButtonItem = [[[UIButton alloc] init] autorelease];
        }
        

        编辑:

        抱歉,您想要一个普通的 UIBarButtonItem。我不相信你可以用 UINavigationBar 做到这一点。您可以尝试添加一个 UserInteractionEnabled UILabel 作为 rightBarButtonItem,我不确定它是否会起作用。

        目前看来这是不可能的。
        sbwoodside has a solution

        【讨论】:

        • 另外,rightBarButtonItems 需要一个 UIBarButtonItem 作为对象类型
        • 真的吗?该死的,我知道你可以在标题中添加任何你想要的东西。这真是令人失望。
        • @property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem; hm,还是我解释错了?
        • 不,你说得对,我只是没注意到,因为我总是使用带边框的按钮。
        【解决方案6】:

        为什么不试试 UI7Kit?适合我,易于使用。

        【讨论】:

          【解决方案7】:
              UIImage *img = [UIImage imageNamed:@"white_star.png"];
          
                CGSize itemSize = CGSizeMake(20, 20);
                UIGraphicsBeginImageContext(itemSize);
                CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
                [img drawInRect:imageRect];
                img = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
          
          
            UIBarButtonItem *barButton = [[UIBarButtonItem alloc ] initWithImage:img style:UIBarButtonSystemItemAdd target:self action:@selector(favoriteButtonClicked)];
            barButton.style = UIBarButtonItemStyleBordered;
          

          【讨论】:

          • 代码包含错误,不是他要找的。 UIBarButtonSystemItemAdd不是 UIBarButtonItemStyle。
          【解决方案8】:

          试试这个,

          UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(didPressButton)];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-03-17
            • 1970-01-01
            • 2017-11-16
            • 2020-03-13
            • 1970-01-01
            • 2011-02-17
            • 1970-01-01
            相关资源
            最近更新 更多