我正在以编程方式向我的UIViewController添加一个UIToolbar。
我遇到的问题是,当我的视图被创建时,工具栏“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;
}
答案 0 :(得分:0)
好的,如果这是你的viewController,你不想在init方法中创建所有的视图层次结构,而是使用viewDidLoad
方法。 (并且您不必自己设置self.view
,UIViewController
中的默认实现会为您执行此操作)
此外,您正在创建UINavigationBar
和UIToolBar
,当您将工具栏添加为子视图时,为两者设置相同的框架...(我会考虑这个UINavigationBar
垃圾代码也是如此)
- (void)viewDidLoad
{
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;
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 350, 44)];
toolbar.barStyle=UIBarStyleBlack;
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"Button 1 text"
style:UIBarButtonItemStyleBordered target:self
action:@selector(buttononepressed:)];
UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"Button 2 text"
style:UIBarButtonItemStyleBordered target:self
action:@selector(button2pressed:)];
// using Objective-C litterals you shorter code than using NSMutableArray !
[toolbar setItems:@[button1, spacer1, button2] animated:NO];
[self.view addSubview:toolBar];
}
}
通常情况下,这就是你所需要的(不需要拨打setNeedsDisplay
,这些是在添加子视图时自动完成的)