我正在以编程方式将 UIToolbar 添加到我的 UIViewController 中。
我遇到的问题是,当我的视图被创建时,工具栏中的按钮“DO”会显示,但它们不会显示它们的标题文本。
如果我等待 10 - 20 秒甚至更长的时间,最终会显示标题。
我尝试调用 setNeedsDisplay 和 setNeedsLayout 来强制 UI 哟更新,但它直接不起作用。
我在做一些可笑的事情吗?我一生都无法弄清楚为什么标题没有立即显示。
-(id)initWithFrame:(CGRect)frame
{
self= [super init];
if (self) {
UIView *view=[[UIView alloc]initWithFrame:frame];
self.view=view;
cellLoader = [UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]] ;
self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y+44,
self.view.frame.size.width,
self.view.frame.size.height-44)
style:UITableViewStylePlain];
[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"mycell"];
[self.view addSubview:self.tableView];
self.tableView.dataSource=self;
self.tableView.delegate=self;
//[self.tableView setContentInset:UIEdgeInsetsMake(44, 0, 0, 0)];
navItem = [[UINavigationItem alloc] init];
UILabel* lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(-200,50,100,40)];
[lbNavTitle setBackgroundColor:[UIColor clearColor]];
lbNavTitle.textAlignment = NSTextAlignmentLeft;
[lbNavTitle setTextColor:[UIColor whiteColor]];
lbNavTitle.text = NSLocalizedString(@"",@"");
navItem.titleView = lbNavTitle;
naviBar = [[UINavigationBar alloc] init];
naviBar.barStyle=UIBarStyleBlack;
naviBar.items = [NSArray arrayWithObject:navItem];
naviBar.frame = CGRectMake(0.0, 0, self.view.frame.size.width, 44.0);
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 350, 44)];
toolbar.barStyle=UIBarStyleBlack;
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:7];
button1=[[UIBarButtonItem alloc]
initWithTitle:@"Button 1 text"
style:UIBarButtonItemStyleBordered target:self
action:@selector(buttononepressed:)];
[buttons addObject: button1];
UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer1];
button2=[[UIBarButtonItem alloc]
initWithTitle:@"Button 2 text"
style:UIBarButtonItemStyleBordered target:self
action:@selector(button2pressed:)];
[buttons addObject:button2];
[toolbar setItems:buttons animated:NO];
[self.view addSubview:toolBar];
[naviBar setNeedsDisplay];
[toolbar setNeedsDisplay];
[self.view setNeedsDisplay];
}
return self;
}