1

我正在以编程方式将 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;

}
4

1 回答 1

0

好的,如果这是您的 viewController,您不想在 init 方法中创建所有视图层次结构,而是使用该viewDidLoad方法。(你不必self.view自己设置,默认实现UIViewController为你做)

此外,您正在创建一个UINavigationBarAND a 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,这些是在添加子视图时自动完成的)

于 2013-09-03T16:14:05.077 回答