在viewDidAppear之前以编程方式定位UIView

sco*_*ski 1 cocoa-touch objective-c ios

我有两个UIViews.第一个包含可变数量的UIButton,可能会溢出two rows.第二个需要定位在第一个之下.

我已经阅读了很多这样的帖子:如何定位UIView?

不幸的是,如果您在viewDidAppear:方法中或之后调用它们,这些似乎只能起作用.这意味着我的视图加载和元素正确定位之间的时间很短.

我怎样才能优雅地解决这个问题?

编辑1:这是一个简单的方法,我正在使用adjust view2向下

-(void)adjustView:(UIView *)view down:(int)down{
    CGRect frame = view.frame;
    frame.origin.y = frame.origin.y + down;
    view.frame = frame;
    }
Run Code Online (Sandbox Code Playgroud)

编辑2:这就像我如何添加我的按钮,调整我的第一个视图,以及计算下一个需要下降的距离

- (void)addButtonsToView:(NSArray *)buttonStrings {
    //To correctly position
    float previousTagsWidth = 0.0;
    self.adjustDown = 0.0;

    for (NSString *str in buttonStrings) {      
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setTitle:str forState:(UIControlState)UIControlStateNormal];

        CGSize size = [str sizeWithFont:TAG_FONT];
        if (previousTagsWidth + size.width > 300) {
            previousTagsWidth = 0;
            self.adjustDown += size.height+10;
            }

        button.frame = CGRectMake(previousTagsWidth, self.adjustDown, size.width+10, size.height+5);
        previousTagsWidth += size.width+20;

        [self.view1 addSubview:button];
        }
    CGRect frame = self.view1.frame;
    frame.size.height = frame.size.height+self.adjustDown;
    self.view1.frame = frame;
    }
Run Code Online (Sandbox Code Playgroud)

Cyr*_*lle 5

为什么不在viewWillLayoutSubviewsviewDidLayoutSubviews?这就是我一直用的东西.

(别忘了打电话super!)