如何以编程方式创建此工具栏

我真的很难以编程方式创建UIToolBar (没有xib文件)。 这就是我想要创造的。

但到现在为止,我觉得我没有以正确的方式接近它。 我尝试过添加一个barbuttonitem,但我不能创建效果,如png所示。 我应该怎么做呢。 需要帮助。

编辑:我添加了更大的图像

 UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque]; UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"toolbar title" style:UIBarButtonItemStylePlain target:self action:@selector(onToolbarTapped:)]; NSArray *toolbarItems = [NSArray arrayWithObjects:spaceItem, customItem, spaceItem, nil]; [self setToolbarItems:toolbarItems animated:NO]; 

使用CGRECT创建工具栏,将其放在您想要的位置

 UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, height)]; 

您可以使用背景图像,标题,文本颜色,背景颜色,…自定义它。

接下来创建您的按钮

 UIBarButtonItem *theButton = [UIBarButtonItem alloc] initWithTitle:@"button title" style:UIBarButtonItemStylePlain target:self action:@selector(selector:)]; 

将其添加到您的工具栏:

 NSArray *buttons = [NSArray arrayWithObjects: theButton, nil]; [myToolbar setItems:buttons animated:NO] 

将工具栏添加到当前视图

 [self.view addSubview:mytoolbar] 

只是键入未测试,希望它会有所帮助

ToolBar和按钮具有自定义图像

 - (void)viewDidLoad { [super viewDidLoad]; UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)]; [myToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar_40.png"]] autorelease] atIndex:0]; UIBarButtonItem *barButtonDone = [self createImageButtonItemWithNoTitle:@"button_down.png" target:self action:@selector(closeMe:)]; UIBarButtonItem *barButtonShare = [self createImageButtonItemWithNoTitle:@"button_share.png" target:self action:@selector(actionShareButton:)]; UIBarButtonItem *barButtonFlexibleGap = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease]; myToolbar.items = [NSArray arrayWithObjects:barButtonDone,barButtonFlexibleGap,barButtonShare,nil]; [self.view addSubview:myToolbar]; [myToolbar release]; } -(void)closeMe:(id)sender { NSLog(@"closeMe"); } -(void)actionShareButton:(id)sender { NSLog(@"actionShareButton"); } -(UIBarButtonItem *)createImageButtonItemWithNoTitle:(NSString *)imagePath target:(id)tgt action:(SEL)a { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *buttonImage = [[UIImage imageNamed:@"button_slice.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0]; UIImage *buttonPressedImage = [[UIImage imageNamed:@"button_slice_over.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0]; CGRect buttonFrame = [button frame]; buttonFrame.size.width = 32; buttonFrame.size.height = 32; [button setFrame:buttonFrame]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)]; imageView.image = [UIImage imageNamed:imagePath]; [button addSubview:imageView]; [imageView release]; [button setBackgroundImage:buttonImage forState:UIControlStateNormal]; [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted]; [button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; return [buttonItem autorelease]; } 

需要添加这些png文件

  1. toolbar_40.png用于工具栏图像
  2. 按钮图像的button_down.png
  3. button_share.png用于按钮图像
  4. button_slice.png用于按钮背景
  5. 按钮背景的button_slice_over.png(按下)
 UIToolbar *toolbar = [UIToolbar new]; toolbar.barStyle = UIBarStyleBlackTranslucent; toolbar.backgroundColor = [UIColor clearColor]; 

然后将UIBarButtonItem添加到toolbar

 UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)]; myToolbar.barStyle = UIBarStyleBlack; UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; myLabel.text = @"Sperre aufheben"; myLabel.textColor = [UIColor whiteColor]; myLabel.textAlignment = UITextAlignmentCenter; myLabel.backgroundColor = [UIColor clearColor]; [myToolbar addSubview:myLabel]; [self.view addSubview:myToolbar]; [myLabel release]; [myToolbar release]; 

看起来更适合导航栏(至少如果你保持你正在做的精神:IE在屏幕底部添加一个标题)

无论如何,我创建了一个简单的测试项目,这就是我为了获得图片所示的function所做的,你可能需要以不同的方式将它插入到视图/控制器中,但它可以获得你想要的相对简单并且不使用按钮栏

 - (void)viewDidLoad { [super viewDidLoad]; UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, 320, 44)]; [navBar setBarStyle:UIBarStyleBlack]; UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"Your Title"]; NSArray *array = [[NSArray alloc] initWithObjects:title, nil]; [navBar setItems:array]; [self.view addSubview:navBar]; }