代码之家  ›  专栏  ›  技术社区  ›  Steve Madsen

如何使用相对点以编程方式定位视图?

  •  5
  • Steve Madsen  · 技术社区  · 14 年前

    当超级视图的边界未知时,相对于超级视图的大小定位视图的最佳方法是什么?

    如果可能的话,我尽量避免硬编码坐标。也许这是愚蠢的,如果是这样,这是一个完全可以接受的答案。

    我在使用自定义UI时遇到过很多次。最近的一个例子是我试图替换 UINavigationItem 带有自定义视图的纯文本标题。我想让那个视图填满superview,但是另外,我想 UIActivityIndicatorView

    - (void) viewDidLoad
    {
        [super viewDidLoad];
    
        customTitleView = [[UIView alloc] initWithFrame:CGRectZero];
        customTitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
        titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        titleLabel.numberOfLines = 2;
        titleLabel.minimumFontSize = 11.0;
        titleLabel.font = [UIFont systemFontOfSize:17.0];
        titleLabel.adjustsFontSizeToFitWidth = YES;
        [customTitleView addSubview:titleLabel];
    
        spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        spinnerView.center = CGPointMake(customTitleView.bounds.size.width - (spinnerView.bounds.size.width / 2) - 2,
                                         customTitleView.bounds.size.height / 2);
        spinnerView.hidesWhenStopped = YES;
        [customTitleView addSubview:spinnerView];
    
        self.navigationItem.titleView = customTitleView;
        [customTitleView release];
    }
    

    customTitleView.bounds 仍然是零。自动调整大小掩码还没有机会完成它的工作,但是我非常想要这些值,这样我就可以计算其他子视图(这里是活动指示器)的相对位置。

    这不难看吗?

    1 回复  |  直到 14 年前
        1
  •  4
  •   cduhn    14 年前

    customTitleView.bounds的宽度和高度为零的唯一原因是您已经使用CGRectZero对其进行了初始化。可以使用任何非零大小初始化视图,然后根据该任意大小定义其子视图。只要您正确定义了子视图的自动调整大小行为,当超级视图的框架在运行时发生变化时,它们的布局就会得到适当的调整。

    例如:

    - (void) viewDidLoad
    {
        [super viewDidLoad];
    
        customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
        customTitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
        titleLabel = [[UILabel alloc] initWithFrame:customTitleView.bounds];
        titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        titleLabel.numberOfLines = 2;
        titleLabel.minimumFontSize = 11.0;
        titleLabel.font = [UIFont systemFontOfSize:17.0];
        titleLabel.adjustsFontSizeToFitWidth = YES;
        [customTitleView addSubview:titleLabel];
        [titleLabel release];
    
        spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        spinnerView.center = CGPointMake(customTitleView.bounds.size.width - (spinnerView.bounds.size.width / 2) - 2,
                                         customTitleView.bounds.size.height / 2);
        spinnerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        spinnerView.hidesWhenStopped = YES;
        [customTitleView addSubview:spinnerView];
        [spinnerView release];
    
        self.navigationItem.titleView = customTitleView;
        [customTitleView release];
    }