zoukankan      html  css  js  c++  java
  • 控件不显示

     UIView *indicateView = [[UIView alloc]initWithFrame:CGRectMake(0, 33, 20, 2)];
        indicateView.backgroundColor = [UIColor redColor];
        self.indicatorView = indicateView;
       
        //子选项
        CGFloat width = self.view.width / 5;
        for (int i = 0; i < self.titles.count; i++) {
            
            UIButton *tap = [UIButton buttonWithType:UIButtonTypeCustom];
            tap.tag = i;
            tap.backgroundColor = kRandomColor;
            tap.frame = CGRectMake(i * width, 0, width, 35);
            [tap setTitle:self.titles[i] forState:UIControlStateNormal];
            [tap setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [tap addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
            [titleView addSubview:tap];
            
            if (i == 0) {
                tap.titleLabel.backgroundColor = [UIColor yellowColor];
                //[tap.titleLabel sizeToFit];
                //[tap layoutIfNeeded];
                NSLog(@"%f,%f",tap.titleLabel.width,tap.centerX);
                self.indicatorView.width = tap.titleLabel.width; //原因在这句,此时tap.titleLabel.width = 0,所以要强制布局,用sizeToFit或layoutIfNeeded
               self.indicatorView.centerX = tap.centerX;
                
            }
        }

    控件创建出来设置了frame,在某处用了该控件的尺寸,但是系统也许在这个时候还没有计算该控件的尺寸什么的,所以这个时候就要强制系统计算布局。

  • 相关阅读:
    c# 泛型委托
    c# 用户自定义转换
    c# 溢出上下文检测
    c# 接口相同方法申明使用
    c# 事件的订阅发布Demo
    c# 匿名方法几种表现形式
    c# 数组协变
    c# 交错数组
    C# 位运算详解
    ABAP 7.4 新语法-内嵌生命和内表操作(转)
  • 原文地址:https://www.cnblogs.com/yintingting/p/4546881.html
Copyright © 2011-2022 走看看