MGBoxKit
MGBoxKit copied to clipboard
Create ability to set layout direction
At the moment I want to scroll only horizontally.
but after calling:
[self.scroller layoutWithSpeed:0.3 completion:nil];
My scroll views content size width is set to standard width for detail view which is 706 pixels.
But my Grid has a greater width and it is then not horizontally scrollable.
I was forced to manually set scrollable area (contentSize) after the layoutWithSpeed method.
[Code below]
self.scroller.contentSize = (CGSize){2000,655};
self.scroller.contentLayoutMode = MGLayoutGridStyle;
self.scroller.bottomPadding = 2;
visibleRect.origin = self.scroller.contentOffset;
visibleRect.size = self.scroller.contentSize;
NSLog(@"Scroller Init: %@",NSStringFromCGRect(visibleRect));
MGBox *grid = [MGBox boxWithSize:(CGSize){2000,400}];
grid.contentLayoutMode = MGLayoutGridStyle;
[self.scroller.boxes addObject:grid];
MGBox *box;
for (int i = 0; i < 50; i++) {
if(i==0 || i==6){
box = [MGBox boxWithSize:(CGSize){50,50}];
box.backgroundColor = [UIColor blueColor];
}else{
box = [MGBox boxWithSize:(CGSize){100, 100}];
box.backgroundColor = [UIColor lightGrayColor];
}
box.leftMargin = box.topMargin = 10;
[grid.boxes addObject:box];
}
[grid layoutWithSpeed:0.3 completion:nil];
//Scroller Size is set here progrmatically
[self.scroller layoutWithSpeed:0.3 completion:nil];
visibleRect.origin = self.scroller.contentOffset;
visibleRect.size = self.scroller.contentSize;
NSLog(@"Layout Speed Method: %@",NSStringFromCGRect(visibleRect));
//Manually Set Scrollers size
//self.scroller.contentSize = (CGSize){2000,655};
[self.scroller scrollToView:grid withMargin:10];
Yeah, horizontally expanding layouts aren't built in yet. The two built in layout algorithms (grid and table) are designed to expand vertically. They base their content width off the width of the container (eg the parent MGScrollView or MGBox).
It probably wouldn't be a big task to add a new layout algorithm for horizontally expanding grids. Either configurable with a new contentLayoutMode
value, or perhaps a boolean to distinguish between vertically expanding and horizontally expanding. The logic would be essentially the same, just with a handful of if conditions and CGFloat additions twiddled around.
If you want to have a crack at it, the relevant code is in MGLayoutManager.m
I'll have a crack at it.
@sobri909: to me it makes most sense that this functionality apply only to grid layouts. Is there any reason for this to be adapted for table layouts as well?
I'd prefer to eventually have it available for both layout modes. There are some use cases. But no sense in building something you don't need, so if it's not useful to you, I wouldn't waste time coding it up.
Note that layout code is currently duplicated between the box provider system and the older layout API. It's not ideal working conditions inside MGLayoutManager
. Again, I'd say only code it up for the layout engine you need it for (ie box provider or old API). Eventually that stuff should be deduplicated and refactored into something more sensible. Though it's fiddly stuff.