问题
I am really having a hard time creating a UIToolBar
programmatically(no xib file). This is what I would like to create.
But till now, I feel I have not approached it in the right way. I have tried adding a barbuttonitem to it, but I cant create the effect as shown in the png. How should I go about it. Help needed.
Edit: I have added a bigger image
回答1:
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];
回答2:
Create your toolbar with CGRECT to put it where you want
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, height)];
You can customize it with background image, title, text color, background color, ...
Next create your button
UIBarButtonItem *theButton = [UIBarButtonItem alloc] initWithTitle:@"button title" style:UIBarButtonItemStylePlain target:self action:@selector(selector:)];
Add it to yout toolbar :
NSArray *buttons = [NSArray arrayWithObjects: theButton, nil];
[myToolbar setItems:buttons animated:NO]
add your toolbar to the current view
[self.view addSubview:mytoolbar]
Just typed not tested, hope it'll help
回答3:
ToolBar and buttons have custom images
- (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];
}
These png files need to be added
- toolbar_40.png for toolbar image
- button_down.png for button image
- button_share.png for button image
- button_slice.png for button background
- button_slice_over.png for button background (pressed)
回答4:
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.backgroundColor = [UIColor clearColor];
Then add a UIBarButtonItem
to toolbar
.
回答5:
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];
回答6:
Seems like a better fit for a nav bar (at least if you are keeping with the spirit of what you are doing: IE adding a title to the bottom of the screen)
Anyway I created a simple test project and this is what I did to get the functionality pictured, you may have to insert it into the view/controller differently but it gets what you want relatively easy and doesn't use the button bar
- (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];
}
来源:https://stackoverflow.com/questions/10500708/how-to-create-this-toolbar-programmatically