gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:22:11 25 4
gpt4 key购买 nike

我创建了一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com