如何在UIToolBar中添加条形按钮 [英] How to add bar buttons in a UIToolBar

查看:85
本文介绍了如何在UIToolBar中添加条形按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 UIToolBar ,并希望添加三个项目,如联系人,日期和消息。我尝试了但是我无法做到这一点。因为我是 Objective C 的新手,所以请帮忙。这是我的ViewController.m

I have created a UIToolBar and want to add three items in that like contact, date and message. I tried but i am not able to do that.Kindly help as i am new to Objective C. Here is my "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


推荐答案

在下面的代码中我添加了两个 UIBarButton ,其中包含 flexSpace ..

您可以添加UIBarButton作为你想要

In Following code i added two UIBarButton with flexSpace..
you can add UIBarButton as you want

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
}

这篇关于如何在UIToolBar中添加条形按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆