Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I programmatically position a view using relative points?
    primarykey
    data
    text
    <p>What is the best way to position a view relative to the size of its superview, when the bounds of the superview are not yet known?</p> <p>I am trying to avoid hard-coding coordinates if it is at all possible. Perhaps this is silly, and if so, that's a perfectly acceptable answer.</p> <p>I've run into this many times when working with custom UI. The most recent example is that I'm trying to replace the <code>UINavigationItem</code> plain-text title with a custom view. I want that view to fill the superview, but in addition, I want a <code>UIActivityIndicatorView</code> on the right side, inset about 2 pixels and centered vertically. Here's the code:</p> <pre><code>- (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]; } </code></pre> <p>Here's my problem: at the time that this code runs, <code>customTitleView.bounds</code> is still zeroes. The auto-resizing mask hasn't had a chance to do its thing yet, but I very much want those values so that I can compute the relative positions of other sub-views (here, the activity indicator).</p> <p>Is this possible without being ugly?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload