A UIView subclass managing subviews. Size and position is calculated according to given weights.
This classes are best installed using Cocoapods. Add the following to your Podfile:
pod 'MWProportionalLayoutView', :git=> 'https://github.com/zliw/MWProportionalLayoutView.git'MWProportionalLayoutView was written for ARC and requires iOS 7.0+ (but it should also work on earlier versions).
An example of the usage can be seen in the MWProportionalLayoutView-Example-Folder. But currently usage is quite simple:
NSArray *myListOfViews = @[[UIView new], [UIView new], [UIView new]];
NSArray *myListOfWeights = @[@(4), @(2), @(1)];
MWProportionalLayoutView *layoutView = [[MWProportionalLayoutView alloc] initWithFrame:myFrame];
[layoutView setSubviews:myListOfViews withWeights:myListOfWeights];
In a real world example it would make sense to configure a different look on the views (e.g. background color, labels, etc).
This is, what the example application looks like :
The current layout algorithm is fixed, but I surely will add some configuration to exchange/configure it later on.
It works in the following way:
- Sort views by their weight
- Pop top-most view/weight
- Set rectangle to the whole area
- Is the rectangle wider than tall?
- If Yes: split up horizontally by percentage given by the weight
- If No: split up vertically by percentage given by the weight
- The rectangle is whatever not taken by the view
- Goto 4
