繁体   English   中英

UIBarButton标题仅定期显示

[英]UIBarButton Title Only Shows Periodically

我以编程方式将UIToolbar添加到我的UIViewController中。

我的问题是,创建视图时,工具栏“ DO”中的按钮会显示,但它们不会显示标题文本。

如果我等待10-20秒甚至更长的时间,标题将最终显示出来。

我尝试调用setNeedsDisplay和setNeedsLayout来强制UI yo更新,但是它无法正常工作。

我在做一些荒谬的事情吗? 我一辈子都无法弄清楚为什么标题没有立即显示。

-(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;

}

好的,如果这是您的viewController,则您不想在init方法中创建所有视图层次结构,而是使用viewDidLoad方法。 (而且您不必自己设置self.viewUIViewController中的默认实现为您完成了此操作)

另外,您正在创建UINavigationBarUIToolBar ,当您仅将工具栏添加为子视图时,将两者设置为相同的框架...(我也会考虑此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 ,添加子视图时这些会自动完成)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM