プログラムで作成するのに本当に苦労してい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:)];
それをyoutツールバーに追加します:
NSArray *buttons = [NSArray arrayWithObjects: theButton, nil];
[myToolbar setItems:buttons animated:NO]
現在のビューにツールバーを追加します
[self.view addSubview:mytoolbar]
テストせずに入力しただけです。お役に立てば幸いです。
ツールバーとボタンにはカスタム画像があります
- (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ファイルを追加する必要があります
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が画面の下部にタイトルを追加します)
とにかく、私は簡単なテストプロジェクトを作成しました。これは、機能を描写するために行ったものです。ビュー/コントローラーに別の方法で挿入する必要があるかもしれませんが、比較的簡単に必要なものを取得し、ボタンバーを使用しません。
- (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];
}