tombenner/nui

View size with autolayout constraints

Closed this issue · 3 comments

Hi, i am new to NUI framework and don't know all architecture of project for now, i am can't setup view size, that use autolayout constraints, from .nss theme. Seems to be that functionality not implemented yet. Or i am missing something?
So i add autolayout handling code into NUIViewRenderer. Test it with simple UIButton. It works.

+ (void)renderSize:(UIView*)view withClass:(NSString*)className
{
    NSArray <NSLayoutConstraint *> *viewConstraints = [view constraints];
    BOOL viewUseAutolayout = viewConstraints != nil && viewConstraints;
    if (viewUseAutolayout){
        if ([NUISettings hasProperty:@"height" withClass:className]) {
            CGFloat height = [NUISettings getFloat:@"height" withClass:className];
            //Or use custom identifier string for example NUIHeightConstraint
            NSPredicate *heightConstraintPredicate = [NSPredicate predicateWithFormat:@"self.firstItem == %@ && self.firstAttribute == %d && self.secondAttribute == %d",view,NSLayoutAttributeHeight,NSLayoutAttributeNotAnAttribute];
            NSLayoutConstraint *heightConstraint = [[viewConstraints filteredArrayUsingPredicate:heightConstraintPredicate] firstObject];
            if(heightConstraint != nil){
                [heightConstraint setConstant:height];
            }
        }
        if ([NUISettings hasProperty:@"width" withClass:className]) {
             CGFloat width = [NUISettings getFloat:@"width" withClass:className];
            //Or use custom identifier string for example NUIWidthConstraint
            NSPredicate *widthConstraintPredicate = [NSPredicate predicateWithFormat:@"self.firstItem == %@ && self.firstAttribute == %d && self.secondAttribute == %d",view,NSLayoutAttributeWidth,NSLayoutAttributeNotAnAttribute];
            NSLayoutConstraint *widthConstraint = [[viewConstraints filteredArrayUsingPredicate:widthConstraintPredicate] firstObject];
            if(widthConstraint != nil){
              [widthConstraint setConstant:width];
            }
        }
    } else{
        CGFloat height = view.frame.size.height;
        if ([NUISettings hasProperty:@"height" withClass:className]) {
            height = [NUISettings getFloat:@"height" withClass:className];
        }
        CGFloat width = view.frame.size.width;
        if ([NUISettings hasProperty:@"width" withClass:className]) {
            width = [NUISettings getFloat:@"width" withClass:className];
        }
        if (height != view.frame.size.height || width != view.frame.size.width) {
            view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, width, height);
        }
    }
}

How do you think will this solution work properly ?

NUI does not support sizing its views with auto layout from the .nss file. This library is meant for styling of colors, fonts, borders and other various details of a view and is not responsible for layout of the view hierarchy itself. Please consult the Style Classes for details on what all can be configured with this library.

Thanks.

This works great!
If NUI already has style properties for width and height, I see no reason that it shouldn't set AutoLayout constraints instead of frames if they already exist