UILabelをNavigationBarのrightBarButtonItemとして作成する方法を教えてもらえますか?
UILabelを使用してUIViewでUIBarButtonItemのinitWithCustomViewを実行しようとしましたが、クラッシュします。
UILabelをNavigationBarのrightBarButtonItemとして作成する方法を教えてもらえますか?
UILabelを使用してUIViewでUIBarButtonItemのinitWithCustomViewを実行しようとしましたが、クラッシュします。
次のコードを試すことができます。
UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(260,6,55,32)];
yourLabel.text = @"Ashu";
[self.navigationController.navigationBar addSubview:yourLabel];
動作する場合はお知らせください。
サンプルコードは次のとおりです。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 65.0, 30.0)];
label.text = @"Text";
barButton = [[UIBarButtonItem alloc] initWithCustomView:label];
次に、「barButton」を右側のナビゲーションアイテムと同じに設定するだけです。
これを行うために:
yourButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30) ];
[yourButton addTarget:self action:@selector(yourmethod:) forControlEvents:UIControlEventTouchUpInside];
yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 8, 40, 30)];
yourLabel.text = @"your Text";
yourLabel.backgroundColor = [UIColor clearColor];
yourLabel.textColor = [UIColor whiteColor];
yourLabel.font = [UIFont boldSystemFontOfSize:12.0f];
[yourLabel sizeToFit];
[yourButton addSubview:yourLabel];
[yourButton sizeToFit];
yourBarButton = [[UIBarButtonItem alloc] initWithCustomView:yourButton];
self.navigationItem.rightBarButtonItem = yourBarButton;