ISBX / apprtc-ios

A native iOS video chat app based on WebRTC
BSD 3-Clause "New" or "Revised" License
1.34k stars 411 forks source link

View size with autolayout constraints #25

Closed sdkdimon closed 8 years ago

sdkdimon commented 8 years ago

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 ?

sdkdimon commented 8 years ago

Sorry issue intended not for you, its my mistake.