UINavigationControllerのツールバーにカスタムラベルを追加しようとしています。私はこのquestionのために一番上の答えに従ったが、それは私のために働くようには見えないし、なぜか分からない。カスタムテキストは表示されませんが、ボタンはそこにあります。それを押すとハイライト表示されますが、テキストはありません。ツールバーでカスタムラベルを追加できない
ここに私のコードです:
- (void) editToolbar{
NSArray *toolbarItemsCopy = [self.toolbarItems copy];
//set up the label
self.notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width/0.25, 21.0f)];
self.notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:20];
self.notificationsLabel.backgroundColor = [UIColor clearColor];
//self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];
self.notificationsLabel.textColor = [UIColor blueColor];
self.notificationsLabel.text = @"Checking server";
self.notificationsLabel.textAlignment = UITextAlignmentCenter;
//init a button with custom view using the label
UIBarButtonItem *notif = [[UIBarButtonItem alloc] initWithCustomView:self.notificationsLabel];
//add to the toolbarItems
NSMutableArray *toolbarItemsMutableArray = [[NSMutableArray alloc]init];
NSLog(@"toolbar %i", self.toolbarItems.count);
//have to add the custom bar button item in a certain place in the toolbar, it should be the 3rd item
for (int i = 0; i < toolbarItemsCopy.count; i++) {
if (i == 2){
[toolbarItemsMutableArray addObject:notif];
}
NSLog(@"toolbar item %i %@", i,[toolbarItemsCopy objectAtIndex:i]);
[toolbarItemsMutableArray addObject:[toolbarItemsCopy objectAtIndex:i]];
}
if (toolbarItemsCopy.count == 4){
}else{
//remove the previous custom label
[toolbarItemsMutableArray removeObjectAtIndex:3];
}
self.toolbarItems = toolbarItemsMutableArray;
//[self.navigationController.toolbar setItems: toolbarItemsMutableArray animated:YES];
NSLog(@"toolbar %i", self.toolbarItems.count);
}https://stackoverflow.com/questions/ask
これはのNSLogが印刷されたものである。
Catalogue[361:10403] toolbar 4
Catalogue[361:10403] toolbar item 0 <UIBarButtonItem: 0x8a24650>
Catalogue[361:10403] toolbar item 1 <UIBarButtonItem: 0x8a36cd0>
Catalogue[361:10403] toolbar item 2 <UIBarButtonItem: 0x8a36d30>
Catalogue[361:10403] toolbar item 3 <UIBarButtonItem: 0x8a382f0>
Catalogue[361:10403] toolbar 5
をこれは私がそれを解決する方法である:
を私がALLOCしており、内部でUILabelを初期化関数ではなく、ivarを使用します。
UILabel *notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
notificationsLabel.backgroundColor = [UIColor clearColor];
//self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];
notificationsLabel.textColor = [UIColor whiteColor];
notificationsLabel.text = @"Checking server";
notificationsLabel.textAlignment = UITextAlignmentCenter;
NSLogは印刷されていますか? – wizH
@wizHはい彼らはそうです。そして、私はボタンをタップすることができます、テキストだけです。 :) – acecapades