Skip to content
This repository has been archived by the owner on Dec 20, 2017. It is now read-only.

How to use with UIScrollView? #18

Open
gelldur opened this issue Jul 10, 2016 · 2 comments
Open

How to use with UIScrollView? #18

gelldur opened this issue Jul 10, 2016 · 2 comments

Comments

@gelldur
Copy link

gelldur commented Jul 10, 2016

Hello

I'm using this lib to layout all my views. But this library doesn't set measured height and other things. How to use this lib with UIScrollView because there we must set contentSize property :(

@mkloeppner
Copy link
Owner

mkloeppner commented Feb 19, 2017

So if I got the problem correctly, you'd like to layout items with a linear layout vertically for example and then figure out what the height would be for example for 10 items of a height of 20px?

Honestly I agree its a bit tricky and not obvious how to achieve this atm. I will improve that in the future.

In the meantime the following might help:

Actually after calling layout all your childs will be placed accordingly the layout settings that you've provided. Note that you can call layout at any time. Just do it right after you specified all your layout details.
Once you called layout your content size will be the position of the latest items position plus its size.
Now you can use what you've calculated and pass it to UIScrollView's content size property.

@mkloeppner
Copy link
Owner

mkloeppner commented Feb 19, 2017

A small example:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIScrollView *childView = [[UIScrollView alloc] init];
    childView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:childView];
    self.childView = childView;
    
    self.childView.frame = self.view.bounds;
    
    self.rootLayout = [[MKLinearLayout alloc] initWithView:self.childView];
    self.rootLayout.orientation = MKLayoutOrientationVertical;
    
    // Construct your layout
    for (int i = 0; i < 100; i++) {
        UIView *greyView = [[UIView alloc] init];
        greyView.backgroundColor = [UIColor grayColor];
        MKLayoutItem *item = [self.rootLayout addSubview:greyView];
        item.size = CGSizeMake(100.0f, 100.0f);
    }
    
    // Manually let the layout calculate its childs sizes
    [self.rootLayout layout];
    
    MKLayoutItem *item = self.rootLayout.items.lastObject;
    self.childView.contentSize = CGSizeMake(item.subview.frame.origin.x + item.subview.frame.size.width,
                                            item.subview.frame.origin.y + item.subview.frame.size.height);
    
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants