Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto layout fixes #7618

Merged
merged 9 commits into from Mar 8, 2016
4 changes: 2 additions & 2 deletions iphone/Classes/Layout/TiLayoutView.h
Expand Up @@ -57,9 +57,9 @@

+(void)removeConstraints:(UIView*)parent fromChild:(UIView*)child;
-(CGSize)sizeThatFits:(CGSize)size height:(id)height;


-(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds;
-(void)initializeTiLayoutView;

-(CGFloat)heightIfWidthWere:(CGFloat)width;
@end
#endif
113 changes: 79 additions & 34 deletions iphone/Classes/Layout/TiLayoutView.m
Expand Up @@ -23,8 +23,10 @@
#define IS_AUTOFILL TiDimensionIsAutoFill
#define IS_DIP TiDimensionIsDip
#define IS_UNDEFINED TiDimensionIsUndefined

#define TI_VIEWS(...) NSDictionaryOfVariableBindings(__VA_ARGS__)

#define ARGS_NOT_NULL (args != nil && ![args isKindOfClass:[NSNull class]])

static inline NSString* TI_CONSTRAINT_STRING(NSLayoutConstraint* constraint)
{
return [NSString stringWithFormat:@"<%p-%p-%li-%li-%li>",
Expand Down Expand Up @@ -180,11 +182,13 @@ -(void)initializeTiLayoutView
{
if (_initialized) return;
_initialized = YES;
[self setViewName_:NSStringFromClass([self class])];
[self setClipsToBounds:YES];
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
[self setAutoresizingMask:UIViewAutoresizingNone];
[self setHorizontalWrap:YES];
[self setDefaultHeight:TiDimensionAutoFill];
[self setDefaultWidth:TiDimensionAutoFill];
[self setDefaultHeight:TiDimensionAutoSize];
[self setDefaultWidth:TiDimensionAutoSize];
[self.layer addObserver:self forKeyPath:@"position" options:0 context:NULL];
[self.layer addObserver:self forKeyPath:@"bounds" options:0 context:NULL];
}
Expand All @@ -195,9 +199,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
dispatch_async(dispatch_get_main_queue(), ^{

CGRect newRect = self.frame;

if (_isInToolbar && self.translatesAutoresizingMaskIntoConstraints == NO)
{
[self setNeedsLayout];
[self layoutIfNeeded];
[self setTranslatesAutoresizingMaskIntoConstraints:YES];
[[self superview] setNeedsLayout];
[[self superview] layoutIfNeeded];
Expand All @@ -206,8 +211,8 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}
if (_isToolbar)
{
// [_innerView setNeedsLayout];
// [_innerView layoutIfNeeded];
[super setNeedsLayout];
[super layoutIfNeeded];
}
if (!CGRectEqualToRect(newRect, _oldRect))
{
Expand Down Expand Up @@ -335,7 +340,7 @@ -(void)setDefaultWidth:(TiDimension)defaultWidth
-(void)setLeft_:(id)args
{
_tiLayoutConstraint.left = TiDimensionFromObject(args);
_tiLayoutConstraint.left_isSet = YES;
_tiLayoutConstraint.left_isSet = ARGS_NOT_NULL;
_isLeftPercentage = IS_PERCENT(_tiLayoutConstraint.left);
if (_isLeftPercentage && _leftPercentage != _tiLayoutConstraint.left.value) {
_leftPercentage = _tiLayoutConstraint.left.value;
Expand All @@ -345,7 +350,7 @@ -(void)setLeft_:(id)args
-(void)setRight_:(id)args
{
_tiLayoutConstraint.right = TiDimensionFromObject(args);
_tiLayoutConstraint.right_isSet = YES;
_tiLayoutConstraint.right_isSet = ARGS_NOT_NULL;
_isRightPercentage = IS_PERCENT(_tiLayoutConstraint.right);
if (_isRightPercentage && _rightPercentage != _tiLayoutConstraint.right.value) {
_rightPercentage = _tiLayoutConstraint.right.value;
Expand All @@ -355,7 +360,7 @@ -(void)setRight_:(id)args
-(void)setTop_:(id)args
{
_tiLayoutConstraint.top = TiDimensionFromObject(args);
_tiLayoutConstraint.top_isSet = YES;
_tiLayoutConstraint.top_isSet = ARGS_NOT_NULL;
_isTopPercentage = IS_PERCENT(_tiLayoutConstraint.top);
if (_isTopPercentage && _topPercentage != _tiLayoutConstraint.top.value) {
_topPercentage = _tiLayoutConstraint.top.value;
Expand All @@ -365,7 +370,7 @@ -(void)setTop_:(id)args
-(void)setBottom_:(id)args
{
_tiLayoutConstraint.bottom = TiDimensionFromObject(args);
_tiLayoutConstraint.bottom_isSet = YES;
_tiLayoutConstraint.bottom_isSet = ARGS_NOT_NULL;
_isBottomPercentage = IS_PERCENT(_tiLayoutConstraint.bottom);
if (_isBottomPercentage && _bottomPercentage != _tiLayoutConstraint.bottom.value) {
_bottomPercentage = _tiLayoutConstraint.bottom.value;
Expand All @@ -380,7 +385,7 @@ -(void)setWidth_:(id)args
} else {
_tiLayoutConstraint.width = TiDimensionFromObject(args);
}
_tiLayoutConstraint.width_isSet = YES;
_tiLayoutConstraint.width_isSet = ARGS_NOT_NULL;
[self updateWidthAndHeight];
[self updateMargins];
[self setNeedsLayout];
Expand All @@ -394,7 +399,7 @@ -(void)setHeight_:(id)args
} else {
_tiLayoutConstraint.height = TiDimensionFromObject(args);
}
_tiLayoutConstraint.height_isSet = YES;
_tiLayoutConstraint.height_isSet = ARGS_NOT_NULL;
[self updateWidthAndHeight];
[self updateMargins];
[self setNeedsLayout];
Expand All @@ -405,8 +410,8 @@ -(void)setCenter_:(id)args
ENSURE_SINGLE_ARG(args, NSDictionary)
_tiLayoutConstraint.centerX = TiDimensionFromObject([args valueForKey:@"x"]);
_tiLayoutConstraint.centerY = TiDimensionFromObject([args valueForKey:@"y"]);
_tiLayoutConstraint.centerX_isSet = YES;
_tiLayoutConstraint.centerY_isSet = YES;
_tiLayoutConstraint.centerX_isSet = !IS_UNDEFINED(_tiLayoutConstraint.centerX);
_tiLayoutConstraint.centerY_isSet = !IS_UNDEFINED(_tiLayoutConstraint.centerY);
[self updateMargins];

}
Expand Down Expand Up @@ -436,6 +441,47 @@ -(void)animateProperties:(NSDictionary*)properties withDuration:(NSUInteger)mill
} completion:callback];
}

-(CGFloat)heightIfWidthWere:(CGFloat)width
{
if (_tiLayoutConstraint.height_isSet && IS_DIP(_tiLayoutConstraint.height)){
return TiDimensionCalculateValue(_tiLayoutConstraint.height, 1);
}
UIView*parent = [self superview];
if (parent != nil) {
[self removeFromSuperview];
}
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
[dummyView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[dummyView addSubview:self];

[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] addSubview:dummyView];

[self updateWidthAndHeight];
[self layoutChildren];

[dummyView layoutIfNeeded];

CGSize size = [dummyView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
[dummyView removeFromSuperview];
dummyView = nil;

[self removeFromSuperview];

_loaded = NO;
if (parent != nil) {
[parent addSubview:self];
TiLayoutView* viewToUpdate = nil;
if ([parent isKindOfClass:[TiLayoutView class]]) {
viewToUpdate = (TiLayoutView*)parent;
} else {
viewToUpdate = self;
}
[viewToUpdate updateWidthAndHeight];
[viewToUpdate layoutChildren];
}
return size.height;
}

-(void)removeFromSuperview
{
[super removeFromSuperview];
Expand Down Expand Up @@ -520,15 +566,22 @@ -(void)layoutChildren

-(void)didMoveToSuperview
{
if ([[self viewName] isEqualToString:@"this_view"]) {
NSLog(@"break here");
}

UIView *superview = [self superview];
if (superview != nil && !_loaded) {
if ([superview isKindOfClass:[UIToolbar class]] || [superview isKindOfClass:[UINavigationBar class]]) {
_isInToolbar = YES;
}

if ([[self subviews] count] && [[[self subviews] objectAtIndex:0] isKindOfClass:[UIToolbar class]]) {
_isToolbar = YES;
}
for (UIView* v in [self subviews]) {
if (![v isKindOfClass:[TiLayoutView class]]) {
[v setTranslatesAutoresizingMaskIntoConstraints:NO];
[v setAutoresizingMask:UIViewAutoresizingNone];
[self addConstraints:TI_CONSTR(@"V:|[v]|", TI_VIEWS(v))];
[self addConstraints:TI_CONSTR(@"H:|[v]|", TI_VIEWS(v))];
break;
Expand All @@ -539,6 +592,7 @@ -(void)didMoveToSuperview
[self updateWidthAndHeight];
[self layoutChildren];
}

[super didMoveToSuperview];
}

Expand Down Expand Up @@ -571,9 +625,6 @@ -(void)removeAndReplaceConstraints:(NSArray *)constraints

-(void)addConstraint:(nonnull NSLayoutConstraint *)constraint
{
if ([[self viewName] isEqualToString:@"button"]) {
NSLog(@"break here");
}
if (!_constraintsAdded) _constraintsAdded = [NSMutableDictionary dictionary];
NSString* description = TI_CONSTRAINT_STRING(constraint);

Expand All @@ -590,9 +641,6 @@ -(void)addConstraint:(nonnull NSLayoutConstraint *)constraint

-(void)removeConstraint:(nonnull NSLayoutConstraint *)constraint
{
if ([[self viewName] isEqualToString:@"button"]) {
NSLog(@"break here");
}
if (!_constraintsAdded) _constraintsAdded = [NSMutableDictionary dictionary];
NSString* description = TI_CONSTRAINT_STRING(constraint);
NSLayoutConstraint* currentConstraint = [_constraintsAdded valueForKey:description];
Expand All @@ -609,8 +657,6 @@ -(void)updateWidthAndHeight
UIView* superview = [self superview];
if (superview == nil) return;



TiDimension width = _tiLayoutConstraint.width;
TiDimension height = _tiLayoutConstraint.height;
TiDimension left = _tiLayoutConstraint.left;
Expand All @@ -624,16 +670,15 @@ -(void)updateWidthAndHeight
BOOL rightSet = _tiLayoutConstraint.right_isSet;
BOOL topSet = _tiLayoutConstraint.top_isSet;
BOOL bottomSet = _tiLayoutConstraint.bottom_isSet;


if ([[self viewName] isEqualToString:@"THIS_ONE"]){
NSLog(@"Break Here");
}

NSDictionary* viewsDict = TI_VIEWS(self, superview);

if (![superview isKindOfClass:[TiLayoutView class]] && ![self isInToolbar])
{

if ([self isKindOfClass:NSClassFromString(@"TiTableViewRow")]) {
NSLog(@"break here");
}
if ([superview isKindOfClass:[UITableView class]]) {
return;
}
Expand Down Expand Up @@ -703,29 +748,29 @@ -(void)updateWidthAndHeight
}
if (IS_DIP(width)) {
CGFloat value = TiDimensionCalculateValue(width, 1);
[self addConstraints: TI_CONSTR(TI_STRING(@"H:[self(%f)]", value), viewsDict)];
[self addConstraints: TI_CONSTR(TI_STRING(@"H:[self(%f@750)]", value), viewsDict)];
}

if (IS_DIP(height)) {
CGFloat value = TiDimensionCalculateValue(height, 1);
[self addConstraints: TI_CONSTR(TI_STRING(@"V:[self(%f)]", value), viewsDict)];
[self addConstraints: TI_CONSTR(TI_STRING(@"V:[self(%f@750)]", value), viewsDict)];
}



if (![self isInToolbar]) {
if (IS_AUTOFILL(height) || (IS_UNDEFINED(height) && IS_AUTOFILL(_defaultHeight))) {
[superview addConstraints: TI_CONSTR(TI_STRING(@"V:[self(superview@25)]"), viewsDict)];
[superview addConstraints: TI_CONSTR(TI_STRING(@"V:[self(superview@500)]"), viewsDict)];
}

if (IS_AUTOFILL(width) || (IS_UNDEFINED(width) && IS_AUTOFILL(_defaultWidth))) {
[superview addConstraints: TI_CONSTR(TI_STRING(@"H:[self(superview@25)]"), viewsDict)];
[superview addConstraints: TI_CONSTR(TI_STRING(@"H:[self(superview@500)]"), viewsDict)];
}

if (IS_AUTOSIZE(width) || (IS_UNDEFINED(width) && IS_AUTOSIZE(_defaultWidth))) {
[self addConstraints: TI_CONSTR(TI_STRING(@"H:[self(0@20)]"), viewsDict)]; // should try to be 0 width with a very low priority
[superview addConstraints: TI_CONSTR(TI_STRING(@"H:[self(<=superview)]"), viewsDict)];
}
if (IS_AUTOSIZE(height) || (IS_UNDEFINED(height) && IS_AUTOSIZE(_defaultHeight))) {
[self addConstraints: TI_CONSTR(TI_STRING(@"V:[self(0@20)]"), viewsDict)]; // should try to be 0 height with a very low priority
[superview addConstraints: TI_CONSTR(TI_STRING(@"V:[self(<=superview)]"), viewsDict)];
}
}
Expand Down
9 changes: 9 additions & 0 deletions iphone/Classes/TiMediaVideoPlayer.m
Expand Up @@ -14,6 +14,15 @@

@implementation TiMediaVideoPlayer

#ifdef TI_USE_AUTOLAYOUT
-(void)initializeTiLayoutView
{
[super initializeTiLayoutView];
[self setDefaultHeight:TiDimensionAutoFill];
[self setDefaultWidth:TiDimensionAutoFill];
}
#endif

-(id)initWithPlayer:(MPMoviePlayerController*)controller_ proxy:(TiProxy*)proxy_ loaded:(BOOL)loaded_
{
if (self = [super init])
Expand Down
12 changes: 9 additions & 3 deletions iphone/Classes/TiUIActivityIndicator.m
Expand Up @@ -14,16 +14,22 @@

@implementation TiUIActivityIndicator

#ifdef TI_USE_AUTOLAYOUT
-(void)initializeTiLayoutView
{
[super initializeTiLayoutView];
[self setDefaultHeight:TiDimensionAutoSize];
[self setDefaultWidth:TiDimensionAutoSize];
}
#endif

- (id) init
{
self = [super init];
if (self != nil) {
style = UIActivityIndicatorViewStyleWhite;
[self setHidden:YES];

#ifdef TI_USE_AUTOLAYOUT
[self setDefaultWidth:TiDimensionAutoSize];
[self setDefaultHeight:TiDimensionAutoSize];
backgroundView = [[UIView alloc] init];
[self addSubview:backgroundView];
#endif
Expand Down
2 changes: 2 additions & 0 deletions iphone/Classes/TiUIActivityIndicatorProxy.m
Expand Up @@ -40,6 +40,7 @@ -(NSString*)apiName
return @"Ti.UI.ActivityIndicator";
}

#ifndef TI_USE_AUTOLAYOUT
-(TiDimension)defaultAutoWidthBehavior:(id)unused
{
return TiDimensionAutoSize;
Expand All @@ -48,6 +49,7 @@ -(TiDimension)defaultAutoHeightBehavior:(id)unused
{
return TiDimensionAutoSize;
}
#endif

#ifndef TI_USE_AUTOLAYOUT
USE_VIEW_FOR_CONTENT_WIDTH
Expand Down
9 changes: 9 additions & 0 deletions iphone/Classes/TiUIButton.m
Expand Up @@ -18,6 +18,15 @@ @implementation TiUIButton

#pragma mark Internal

#ifdef TI_USE_AUTOLAYOUT
-(void)initializeTiLayoutView
{
[super initializeTiLayoutView];
[self setDefaultHeight:TiDimensionAutoSize];
[self setDefaultWidth:TiDimensionAutoSize];
}
#endif

-(void)dealloc
{
[button removeTarget:self action:NULL forControlEvents:UIControlEventAllTouchEvents];
Expand Down
9 changes: 9 additions & 0 deletions iphone/Classes/TiUIButtonBar.m
Expand Up @@ -11,6 +11,15 @@

@implementation TiUIButtonBar

#ifdef TI_USE_AUTOLAYOUT
-(void)initializeTiLayoutView
{
[super initializeTiLayoutView];
[self setDefaultHeight:TiDimensionAutoSize];
[self setDefaultWidth:TiDimensionAutoSize];
}
#endif

- (id) init
{
self = [super init];
Expand Down
2 changes: 2 additions & 0 deletions iphone/Classes/TiUIButtonBarProxy.m
Expand Up @@ -36,6 +36,7 @@ -(TiUIView*)newView
USE_VIEW_FOR_CONTENT_HEIGHT


#ifndef TI_USE_AUTOLAYOUT
-(TiDimension)defaultAutoWidthBehavior:(id)unused
{
return TiDimensionAutoSize;
Expand All @@ -44,5 +45,6 @@ -(TiDimension)defaultAutoHeightBehavior:(id)unused
{
return TiDimensionAutoSize;
}
#endif

@end