我真的很難以編程方式創建UIToolBar
(沒有xib文件)。這是我想創建的。如何以編程方式創建此工具欄
但直到現在,我覺得我還沒有以正確的方式接近它。我曾嘗試添加一個barbuttonitem,但我不能創建png中顯示的效果。我應該如何去做。需要幫助。
編輯:我添加了一個更大的圖像
我真的很難以編程方式創建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];
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.backgroundColor = [UIColor clearColor];
然後添加一個UIBarButtonItem
到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文件需要被添加
我只需要創建工具欄,如圖所示。我不必在工具欄中添加多個barbuttonitems。 –
「[myToolbar insertSubview:[[[...」這行代碼是在工具欄上插入一個圖像。如果你的圖像(toolbar_40.png)上有文字和漸變(如圖中所示),你應該得到你想要的工具欄。 –
沒有其他辦法,比添加圖像。我沒有看起來與圖片所示圖片相匹配的圖片 –
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];
用的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:)];
將它添加到工具欄YOUT:
NSArray *buttons = [NSArray arrayWithObjects: theButton, nil];
[myToolbar setItems:buttons animated:NO]
工具欄添加到當前視圖
[self.view addSubview:mytoolbar]
剛剛輸入沒有經過測試,希望對大家有所幫助
好像更好的適合導航欄(至少如果你保持你正在做的事情的精神:IE添加標題到屏幕底部)
無論如何,我創建了一個簡單的測試項目,這是我所做的功能,你可能不得不將它插入到視圖/控制器不同,但它得到你想要的相對容易,不使用按鈕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];
}
您能否添加較大的圖片?很難看到我們正在看什麼和你想要完成什麼。對我來說,這看起來像一個帶有標題集的UINavigationBar。 – runmad
我已經更新了圖像 –
什麼工具欄又是什麼? –