Relativelayout or LinearLayout in ios iphone development?

前端 未结 6 654
再見小時候
再見小時候 2021-02-08 15:23

I want to add a subview in the top of my view, I have to recalculate the origin y value for all of other views and re-position them to leave space for the new added view.

<
6条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 15:38

    I've been trying to do a relative (linear) layout for a while and finally decided to just subclass UIScrollView to get it done.

    I started out just replacing layoutSubviews with a simple loop through the subviews that reset the origins while keeping a running Y. But, some unexpected things are added to the scrollview, including UIInlineAutoCorrect views from textfields/views, which means these things were being mangled by the layout. So I added a little bit of logic that uses the tag property of a UIView to determine if I should lay it out:

    -(void) layoutSubviews{
        CGFloat runningY = 0.0f;
        CGFloat widestWidth = 0.0f;
        for (UIView *view in self.subviews) {
            if (view.tag != 1999) {
                continue;
            }
    
            view.origin = CGPointMake(view.origin.x, runningY);
            runningY += view.height;
    
            if ([view autoresizingMask] == UIViewAutoresizingFlexibleWidth) {
                view.width = self.width;
            }
    
    
    
            if (view.width > widestWidth) {
                widestWidth = view.width;
            }
        }
        [self setContentSize:CGSizeMake(widestWidth, runningY)];
    
    }
    

    If you would still like to use unique tags for your views, you should just specify a range of tags that will be included in the layout instead of a single value.

提交回复
热议问题