【问题标题】:Add both UIBarButtonSystemItemAdd and UIBarButtonSystemItemTrash to one side of a Navigation Item or a UISegmentedControl将 UIBarButtonSystemItemAdd 和 UIBarButtonSystemItemTrash 添加到导航项或 UISegmentedControl 的一侧
【发布时间】:2010-01-06 22:05:24
【问题描述】:

我想要一个UIViewController,它在标题右侧有一个添加和一个回收站按钮,可以通过将两个UIBarButtonItems 添加到导航项/栏或将它们添加到UISegmentedControl 和然后将 SegmentedControl 添加到项目中。这可能吗?如果是,如何做到最好?

【问题讨论】:

    标签: iphone cocoa-touch uinavigationbar uisegmentedcontrol uinavigationitem


    【解决方案1】:

    您可以通过将多个按钮包装在UIToolbarsample code 中来向导航项添加多个按钮。

    【讨论】:

      【解决方案2】:

      我做过类似的事情。通过创建一个包含两个 UIButtons 的 UIView 子类,我将两个 UIButtons 添加到导航项/栏。然后你可以这样做:

      MyUIViewSubclass *tempview = [[[MyUIViewSubclass alloc] initWithFrame:CGRectMake(234,4,84,30)] autorelease];
      UIBarButtonItem newButton = [[[UIBarButtonItem alloc] initWithCustomView:tempview] autorelease];
      [self.navigationItem setRightBarButtonItem:newButton animated:NO];
      

      您只需在MyUIViewSubclass 中布置按钮即可。

      另外,我在自定义的 init 命令中传递目标的 ID,以便更轻松地定位视图中的按钮。所以对于MyUIViewSubclass 而不是initWithFrame,我有这样的东西:

      - (id)initWithFrame:(CGRect)aRect andTarget:(id)newTarget {
          if (self = [super initWithFrame:aRect]) {
      
      
      
              UIButton *editbtn = [[[UIButton alloc]  initWithFrame:fframe] autorelease];
              [editbtn addTarget:newTarget action:@selector(MBEdit) forControlEvents:UIControlEventTouchUpInside];
      
              [self addSubview:editbtn];
              [self setFirstbutton:editbtn];
              [editbtn release];
      
      
      
              UIButton *newbtn = [[[UIButton alloc]  initWithFrame:fframe] autorelease];
              [newbtn addTarget:newTarget action:@selector(MBNew) forControlEvents:UIControlEventTouchUpInside];
      
              [self addSubview:newbtn];
              [self setSecondbutton:newbtn];
              [newbtn release];
      
          }
      
          return self;
      
      }
      

      【讨论】:

      • 但我仍然需要自己绘制 UIButton 以使它们看起来像导航栏按钮,对吧?我非常乐意使用 UISegmentedControl(这样可以省去绘画的麻烦)——但我真正需要的是 + 和原来的 iPhone 垃圾桶图标。不过,我更喜欢更优雅的解决方案(比如为粗体 + 和垃圾桶设置一些 Unicode 字母代码)。
      • 我实际上只是截取了一个屏幕截图并将图像用作我的按钮。我几乎可以肯定,您也可以只向视图添加一个分段控件,而不是两个按钮。那也可以。
      猜你喜欢
      • 2014-04-30
      • 2012-12-05
      • 2015-12-31
      • 2017-05-06
      • 1970-01-01
      • 2012-11-18
      • 2017-09-09
      • 2012-03-19
      • 2019-08-14
      相关资源
      最近更新 更多