iphone - 如何在 UIToolBar 中添加条形按钮

标签 iphone ios objective-c ios6

我创建了一个 UIToolBar 并想在其中添加三个项目,如联系人、日期和消息。我试过了,但我做不到。请帮助我,因为我是 Objective C 的新手。这是我的“ViewController.m”

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 414, self.view.frame.size.width, 44);
UIBarButtonItem *contact = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:nil];
UIBarButtonItem *message = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];

NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:contact,message, nil];
[toolbar setItems:items animated:NO];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

@end

最佳答案

在下面的代码中,我添加了两个 UIBarButtonflexSpace..
您可以根据需要添加 UIBarButton

UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    Toolbar.barStyle = UIBarStyleBlackTranslucent;
    [Toolbar sizeToFit];

     NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    [flexSpace release];

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(Cancel)];
    [barItems addObject:btnCancel];

    UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done)];
    [barItems addObject:btnDone];

    [Toolbar setItems:barItems animated:YES];

点击条形按钮时调用以下方法

-(void)Cancel
{
  // Write Code for Cancel Method
}

-(void)done
{
  // Write Code for Done Method
}

关于iphone - 如何在 UIToolBar 中添加条形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14704686/

相关文章:

iphone - 带有粗边框的 ModalView 旋转(如 iBooks 应用程序)

iphone - 以编程方式添加 TabBarController

ios - CSS 固定定位在 IOS 上导致在放大然后缩小后滚动不稳定(使用 fancybox)

ios - Swift & ObjC 桥 - 找不到 "WKNavigationDelegate"的协议(protocol)声明

iphone - 色度计 RGB iPhone ios 代码

ios - 用户不接受 XMPPRoom 中的邀请

objective-c - Apple 允许 "Fake iOS Frameworks"吗?

ios - 对 CLLocationDegrees 对象数组进行排序

ios - 尝试制作简单的 UIImageVIew 动画

objective-c - __weak 和 __block 引用有什么区别?