diff --git a/iphone/Classes/Layout/TiLayoutView.m b/iphone/Classes/Layout/TiLayoutView.m index 2c46bf17a3e..b42e5790e1d 100644 --- a/iphone/Classes/Layout/TiLayoutView.m +++ b/iphone/Classes/Layout/TiLayoutView.m @@ -5,47 +5,6 @@ * Please see the LICENSE included with this distribution for details. */ -#import - -@implementation UIView (Tracking) - -+ (void)load { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - Class class = [self class]; - - SEL originalSelector = @selector(removeConstraint:); - SEL swizzledSelector = @selector(xxx_setConstraints:); - - Method originalMethod = class_getInstanceMethod(class, originalSelector); - Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); - - BOOL didAddMethod = - class_addMethod(class, - originalSelector, - method_getImplementation(swizzledMethod), - method_getTypeEncoding(swizzledMethod)); - - if (didAddMethod) { - class_replaceMethod(class, - swizzledSelector, - method_getImplementation(originalMethod), - method_getTypeEncoding(originalMethod)); - } else { - method_exchangeImplementations(originalMethod, swizzledMethod); - } - }); -} - -#pragma mark - Method Swizzling - -- (void)xxx_setConstraints:(id)constr { - if ([self isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) { - } - [self xxx_setConstraints:constr]; -} -@end - #ifdef TI_USE_AUTOLAYOUT #import "TiLayoutView.h" #import "TiUtils.h" @@ -492,9 +451,10 @@ -(CGFloat)heightIfWidthWere:(CGFloat)width [self removeFromSuperview]; } UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 0)]; - [dummyView setAutoresizingMask:UIViewAutoresizingNone]; - [dummyView setTranslatesAutoresizingMaskIntoConstraints:NO]; + [dummyView setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; [dummyView addSubview:self]; + + [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] addSubview:dummyView]; [self updateWidthAndHeight]; [self layoutChildren]; @@ -502,8 +462,12 @@ -(CGFloat)heightIfWidthWere:(CGFloat)width [dummyView layoutIfNeeded]; CGSize size = [dummyView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; + [dummyView removeFromSuperview]; + dummyView = nil; + [self removeFromSuperview]; - + + _loaded = NO; if (parent != nil) { [parent addSubview:self]; TiLayoutView* viewToUpdate = nil; @@ -602,7 +566,7 @@ -(void)layoutChildren -(void)didMoveToSuperview { - if ([self isKindOfClass:NSClassFromString(@"TiTableViewRow")]) { + if ([[self viewName] isEqualToString:@"this_view"]) { NSLog(@"break here"); } @@ -628,6 +592,7 @@ -(void)didMoveToSuperview [self updateWidthAndHeight]; [self layoutChildren]; } + [super didMoveToSuperview]; } @@ -783,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@250)]"), 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@250)]"), 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)]; } } @@ -882,13 +847,13 @@ -(void)updateMarginsForAbsoluteLayout:(TiLayoutView*)child [self removeConstraint:[NSLayoutConstraint constraintWithItem:child attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; if (IS_DIP(left)) { - [self addConstraints:TI_CONSTR( TI_STRING( @"H:|-(%f@250)-[child]", leftValue), viewsDict)]; + [self addConstraints:TI_CONSTR( TI_STRING( @"H:|-(%f)-[child]", leftValue), viewsDict)]; } else if (leftSet) { [self removeConstraints:TI_CONSTR( TI_STRING( @"H:|-(%f)-[child]", leftValue), viewsDict)]; } if (IS_DIP(right) && (IS_UNDEFINED(left) || IS_AUTOFILL(width) || IS_AUTOSIZE(width) || IS_UNDEFINED(width))) { - [self addConstraints:TI_CONSTR( TI_STRING( @"H:[child]-(%f@250)-|", rightValue), viewsDict)]; + [self addConstraints:TI_CONSTR( TI_STRING( @"H:[child]-(%f)-|", rightValue), viewsDict)]; } else if (rightSet) { [self removeConstraints:TI_CONSTR( TI_STRING( @"H:[child]-(%f)-|", rightValue), viewsDict)]; } @@ -1044,14 +1009,9 @@ -(void)updateMarginsForVerticalLayout:(TiLayoutView*)prev current:(TiLayoutView* CGFloat prevBottomValue = TiDimensionCalculateValue(prevBottom, 1); [self addConstraints: TI_CONSTR( TI_STRING(@"V:[prev]-(%f)-[child]",(topValue+prevBottomValue)), viewsDict2)]; } - if (next == nil) // last one + if (next == nil && (IS_AUTOFILL(height) || (IS_UNDEFINED(height) && IS_AUTOFILL(child->_defaultHeight)))) // last one { - if (IS_AUTOFILL(height) || (IS_UNDEFINED(height) && IS_AUTOFILL(child->_defaultHeight))) - { - [self addConstraints: TI_CONSTR( TI_STRING(@"V:[child]-(%f)-|",(bottomValue)), viewsDict)]; - } else { - [self addConstraints: TI_CONSTR( TI_STRING(@"V:[child]-(%f@250)-|",(bottomValue)), viewsDict)]; - } + [self addConstraints: TI_CONSTR( TI_STRING(@"V:[child]-(%f)-|",(bottomValue)), viewsDict)]; } else { [self removeConstraints: TI_CONSTR(@"V:[child]-(0)-|", viewsDict)]; } diff --git a/iphone/Classes/TiUITableView.m b/iphone/Classes/TiUITableView.m index 0eada3217f9..cc7cd4b6adb 100644 --- a/iphone/Classes/TiUITableView.m +++ b/iphone/Classes/TiUITableView.m @@ -2485,6 +2485,11 @@ - (NSInteger)tableView:(UITableView *)ourTableView indentationLevelForRowAtIndex -(CGFloat)computeRowWidth { CGFloat rowWidth = tableview.bounds.size.width; +#ifdef TI_USE_AUTOLAYOUT + if (rowWidth == 0) { + rowWidth = [[[[[UIApplication sharedApplication] delegate] window] rootViewController] view].bounds.size.width; + } +#endif // Apple does not provide a good way to get information about the index sidebar size // in the event that it exists - it silently resizes row content which is "flexible width" @@ -2509,8 +2514,15 @@ -(CGFloat)computeRowWidth return rowWidth; } +#ifdef TI_USE_AUTOLAYOUT +-(CGFloat)tableView:(UITableView*)ourTableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath +{ + return 45; +} +#endif + - (CGFloat)tableView:(UITableView *)ourTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath -{ +{ NSIndexPath* index = indexPath; if (ourTableView != tableview) { index = [self indexPathFromSearchIndex:[indexPath row]]; diff --git a/iphone/Classes/TiUITableViewRowProxy.h b/iphone/Classes/TiUITableViewRowProxy.h index ee536c63739..99688eef939 100644 --- a/iphone/Classes/TiUITableViewRowProxy.h +++ b/iphone/Classes/TiUITableViewRowProxy.h @@ -24,7 +24,11 @@ TiDimension topCap; BOOL configuredChildren; int dirtyRowFlags; - UIView * rowContainerView; +#ifdef TI_USE_AUTOLAYOUT + TiLayoutView * rowContainerView; +#else + UIView * rowContainerView; +#endif BOOL modifyingRow; BOOL attaching; NSInteger row; diff --git a/iphone/Classes/TiUITableViewRowProxy.m b/iphone/Classes/TiUITableViewRowProxy.m index ef51ac41e9e..8afdff6c6bc 100644 --- a/iphone/Classes/TiUITableViewRowProxy.m +++ b/iphone/Classes/TiUITableViewRowProxy.m @@ -34,6 +34,10 @@ @interface TiUITableViewRowContainer : UIView { TiProxy * hitTarget; CGPoint hitPoint; +#ifdef TI_USE_AUTOLAYOUT + CGFloat m_height; + CGFloat m_width; +#endif } @property(nonatomic,retain,readwrite) TiProxy * hitTarget; @property(nonatomic,assign,readwrite) CGPoint hitPoint; @@ -126,8 +130,21 @@ - (void) dealloc -(void)initializeTiLayoutView { [super initializeTiLayoutView]; - [self setDefaultWidth:TiDimensionAutoFill]; - [self setDefaultHeight:TiDimensionAutoFill]; + [self setDefaultWidth:TiDimensionAutoSize]; + [self setDefaultHeight:TiDimensionAutoSize]; + [self setHeight_:@"SIZE"]; +} + +-(CGFloat)heightIfWidthWere:(CGFloat)width +{ + if (m_width != width) { + m_width = width; + m_height = [super heightIfWidthWere:width]; + } + if (m_height == 0) { + m_height = [super heightIfWidthWere:width]; + } + return m_height; } #endif @@ -220,7 +237,6 @@ -(void)setBackgroundTopCap:(id)value } } -#ifndef TI_USE_AUTOLAYOUT // Special handling to try and avoid Apple's detection of private API 'layout' -(void)setValue:(id)value forUndefinedKey:(NSString *)key { @@ -235,13 +251,16 @@ -(void)setValue:(id)value forUndefinedKey:(NSString *)key return; } } +#ifndef TI_USE_AUTOLAYOUT layoutProperties.layoutStyle = TiLayoutRuleFromObject(value); +#else + [[self currentRowContainerView] setLayout_:value]; +#endif [self replaceValue:value forKey:[@"lay" stringByAppendingString:@"out"] notification:YES]; return; } [super setValue:value forUndefinedKey:key]; } -#endif -(CGFloat)sizeWidthForDecorations:(CGFloat)oldWidth forceResizing:(BOOL)force { CGFloat width = oldWidth; @@ -282,6 +301,7 @@ -(CGFloat)sizeWidthForDecorations:(CGFloat)oldWidth forceResizing:(BOOL)force return width; } + -(CGFloat)rowHeight:(CGFloat)width { if (TiDimensionIsDip(height)) @@ -297,6 +317,9 @@ -(CGFloat)rowHeight:(CGFloat)width if (TiDimensionIsPercent(height) && [self table] != nil) { result = TiDimensionCalculateValue(height, [self table].bounds.size.height); } +#else + result = [(TiLayoutView*)[self currentRowContainerView] heightIfWidthWere:width]; + result = result == 0 ? 0 : result + 1; #endif return (result == 0) ? [table tableRowHeight:0] : result; } @@ -549,9 +572,12 @@ -(UIView*)view } //Private method : For internal use only --(UIView*) currentRowContainerView +-(TiUITableViewRowContainer*) currentRowContainerView { - return rowContainerView; + if (rowContainerView == nil) { + rowContainerView = [[TiUITableViewRowContainer alloc] init]; + } + return (TiUITableViewRowContainer*)rowContainerView; } //Private method :For internal use only. Called from layoutSubviews of the cell. -(void)triggerLayout @@ -642,10 +668,12 @@ -(void)configureChildren:(UITableViewCell*)cell } if (rowContainerView == nil) { rowContainerView = [[TiUITableViewRowContainer alloc] initWithFrame:rect]; - [contentView addSubview:rowContainerView]; } else { [rowContainerView setFrame:rect]; } + if ([rowContainerView superview] == nil) { + [contentView addSubview:rowContainerView]; + } [rowContainerView setBackgroundColor:[UIColor clearColor]]; [rowContainerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; @@ -905,7 +933,6 @@ -(void)setSelectedBackgroundGradient:(id)arg TiThreadPerformOnMainThread(^{[callbackCell setSelectedBackgroundGradient_:newGradient];}, NO); } - -(void)propertyChanged:(NSString*)key oldValue:(id)oldValue newValue:(id)newValue proxy:(TiProxy*)proxy { // these properties should trigger a re-paint for the row diff --git a/iphone/Classes/TiUIView.m b/iphone/Classes/TiUIView.m index 6f40cefe10d..2279a555f50 100644 --- a/iphone/Classes/TiUIView.m +++ b/iphone/Classes/TiUIView.m @@ -211,6 +211,14 @@ -(void)removeFromSuperview } } +-(void)initializeTiLayoutView +{ + [super initializeTiLayoutView]; + if ([self class] == [TiUIView class]) { + [self setDefaultHeight:TiDimensionAutoFill]; + [self setDefaultWidth:TiDimensionAutoFill]; + } +} - (id) init { self = [super init]; diff --git a/iphone/layout/Classes/TiTableView.h b/iphone/layout/Classes/TiTableView.h index 11b94a7b93a..7f986a80fae 100644 --- a/iphone/layout/Classes/TiTableView.h +++ b/iphone/layout/Classes/TiTableView.h @@ -7,6 +7,22 @@ #import "TiLayoutView.h" +@interface TiTableViewRow : UITableViewCell +{ + CGFloat _width; + CGFloat _height; + TiDimension _tiHeight; +} + +@property (nonatomic, retain) TiLayoutView* parentView; + +-(CGFloat)heightFromWidth:(CGFloat)width; +-(void)setHeight_:(id)args; +-(void)setLayout_:(id)args; +@end + @interface TiTableView : TiLayoutView +@property (nonatomic, retain) NSArray* tableData; + @end diff --git a/iphone/layout/Classes/TiTableView.m b/iphone/layout/Classes/TiTableView.m index 89aa0fd5fef..437d1fbfd29 100644 --- a/iphone/layout/Classes/TiTableView.m +++ b/iphone/layout/Classes/TiTableView.m @@ -7,6 +7,62 @@ #import "TiTableView.h" +@implementation TiTableViewRow + +- (instancetype)init +{ + self = [super init]; + if (self) { + _parentView = [[TiLayoutView alloc] init]; + [_parentView setDefaultHeight:TiDimensionAutoSize]; + [_parentView setDefaultWidth:TiDimensionAutoSize]; + [_parentView setWidth:@"SIZE"]; + [_parentView setHeight:@"SIZE"]; + [[self contentView] addSubview:_parentView]; + } + return self; +} + +-(void)setLayout_:(id)args +{ + [_parentView setLayout_:args]; +} + +-(void)setHeight_:(id)args +{ + _tiHeight = TiDimensionFromObject(args); +} +-(void)addSubview:(UIView *)view +{ + if ([view isKindOfClass:[TiLayoutView class]]) { + [_parentView addSubview:view]; + } else { + [super addSubview:view]; + } +} + +-(CGFloat)heightFromWidth:(CGFloat)width +{ + if (TiDimensionIsDip(_tiHeight)) { + return TiDimensionCalculateValue(_tiHeight, 1); + } + + if (TiDimensionIsUndefined(_tiHeight)) { + return 44.0; + } + + if (width != _width) { + _width = width; + _height = [_parentView heightIfWidthWere:width]; + } + if (_height == 0) { + _height = [_parentView heightIfWidthWere:width]; + } + return _height + 8; +} + +@end + @interface TiTableView() { UITableView* _tableView; @@ -16,6 +72,18 @@ @interface TiTableView() @implementation TiTableView +-(void)initializeTiLayoutView +{ + [super initializeTiLayoutView]; + [self setDefaultHeight:TiDimensionAutoFill]; + [self setDefaultWidth:TiDimensionAutoFill]; +} +-(void)setTableData:(NSArray *)tableData +{ + _tableData = tableData; + [_tableView reloadData]; +} + - (instancetype)init { self = [super init]; @@ -26,36 +94,47 @@ - (instancetype)init [_tableView setTranslatesAutoresizingMaskIntoConstraints:NO]; [_tableView setRowHeight:UITableViewAutomaticDimension]; [self addSubview:_tableView]; + [self setBackgroundColor:[UIColor redColor]]; } return self; } +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; +{ + + TiTableViewRow* row = [[self tableData] objectAtIndex: [indexPath row]]; + CGSize currentSize = [self bounds].size; + if (CGSizeEqualToSize(currentSize, CGSizeZero)) { + currentSize = [[UIScreen mainScreen] bounds].size; + } + return [row heightFromWidth:currentSize.width]; +} + +-(void)didMoveToSuperview +{ + [super didMoveToSuperview]; + [_tableView reloadData]; +} + -(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return 35; + NSInteger count = [[self tableData] count]; + return count; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - return 5; + return 1; } -(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { - static NSString* identifier = @"ti_tableview_cell"; - UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier]; - if (cell == nil) - { - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; - [[cell textLabel] setNumberOfLines:0]; - [[cell detailTextLabel] setNumberOfLines:0]; - } - [[cell textLabel] setText: TI_STRING(@"Title for section %li and row %li", (long)[indexPath section], (long)[indexPath row])]; - [[cell detailTextLabel] setText: TI_STRING(@"Subtitle for section %li and row %li", (long)[indexPath section], (long)[indexPath row])]; - [cell setNeedsUpdateConstraints]; - [cell updateConstraintsIfNeeded]; + TiTableViewRow* row = [[self tableData] objectAtIndex: [indexPath row]]; + + [[row parentView] setFrame:[[row contentView] bounds]]; + [[row parentView] performSelector:@selector(updateWidthAndHeight)]; - return cell; + return row; } @end diff --git a/iphone/layout/layout/ViewController.m b/iphone/layout/layout/ViewController.m index 490bc1edf73..53007a5ebe8 100644 --- a/iphone/layout/layout/ViewController.m +++ b/iphone/layout/layout/ViewController.m @@ -9,16 +9,18 @@ #import "TiScrollableView.h" #import "TiScrollView.h" #import "TiTextField.h" -//#import "TiSwitch.h" +#import "TiSwitch.h" #import "TiToolbar.h" -//#import "TiUtils.h" +#import "TiView.h" #import "TiLabel.h" #import "TiButton.h" +#import "TiTableView.h" #define FILL @"FILL" #define SIZE @"SIZE" + @implementation ViewController -(UIColor*) randomColor @@ -35,39 +37,72 @@ - (void)viewDidLoad { [super viewDidLoad]; - dispatch_async(dispatch_get_main_queue(), ^{ - - UIView* thisView = [self view]; - - TiLayoutView* window = [[TiLayoutView alloc] init]; - [window setOnLayout:^(TiLayoutView *s, CGRect r) { - NSLog(@"here"); - }]; - - TiToolbar* toolbar = [[TiToolbar alloc] init]; - [window insertSubview:toolbar atIndex:0]; - - toolbar.backgroundColor = [UIColor grayColor]; - - UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; - UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:@"meh 1" style:UIBarButtonItemStylePlain target:nil action:nil]; - - TiTextField* textField = [[TiTextField alloc] init]; - textField.viewName = @"TiUITextField"; - - button1.width = 33; - - textField.backgroundColor = [UIColor yellowColor]; - textField.width = @200; - textField.height = @32; - - [toolbar setItems:@[flexSpace, button1, flexSpace, textField, flexSpace]]; + TiLayoutView* myView = [[TiLayoutView alloc] init]; + + TiLayoutView* parentView = [[TiLayoutView alloc] init]; + [parentView setWidth:@300]; + [parentView setHeight:@"SIZE"]; + [parentView setLayout_:@"vertical"]; + [parentView setBackgroundColor:[UIColor lightGrayColor]]; + + TiLayoutView* view = [[TiLayoutView alloc] init]; + [view setWidth:@"100"]; + [view setHeight:@"100"]; + [view setViewName:@"this_view"]; + [view setBackgroundColor:[UIColor redColor]]; + + TiLabel* label = [[TiLabel alloc] init]; + [label setText:@"Bacon ipsum dolor amet jowl boudin prosciutto capicola, tail ribeye flank. Pork belly chicken meatloaf picanha chuck frankfurter pig filet mignon jowl ham"]; + + [parentView addSubview:view]; + [parentView addSubview:label]; + + [myView addSubview:parentView]; + [[self view] addSubview:myView]; + + + CGFloat height = [parentView heightIfWidthWere:300]; + NSLog(@"Height: %f", height); + + [parentView setOnLayout:^(TiLayoutView *sender, CGRect rect) { + NSLog(@"Height: %f", rect.size.height); + }]; + + + NSMutableArray* array = [NSMutableArray array]; + // + for (NSInteger i =0; i < 100; i++) { + TiTableViewRow* row = [[TiTableViewRow alloc] init]; + [row setLayout_:@"vertical"]; + [row setHeight_:@"SIZE"]; - [thisView addSubview:window]; - [thisView bringSubviewToFront:window]; - NSLog(@"Done"); - }); + if (i % 2) { + TiLabel* label = [[TiLabel alloc] init]; + [label setText:@"Bacon ipsum dolor amet jowl boudin prosciutto capicola, tail ribeye flank. Pork belly chicken meatloaf picanha chuck frankfurter pig filet mignon jowl ham"]; + TiView* v = [[TiView alloc] init]; + [v setWidth:@"100"]; + [v setHeight:@"100"]; + [v setBackgroundColor: [UIColor redColor]]; + [row addSubview:label]; + [row addSubview:v]; + [[row parentView] setViewName: @"1"]; + } else { + TiLabel* label = [[TiLabel alloc] init]; + [label setText:@"Bacon ipsum dolor amet jowl boudin prosciutto capicola, tail ribeye flank. Pork belly chicken meatloaf picanha chuck frankfurter pig filet mignon jowl ham. Picanha tenderloin rump ball tip."]; + [row addSubview:label]; + [[row parentView] setViewName: @"2"]; + } + [array addObject:row]; + } + + + TiTableView* table = [[TiTableView alloc] init]; + [table setTableData:array]; + + [myView addSubview:table]; + [[self view] addSubview:myView]; + } @end diff --git a/iphone/layout/layoutTests/layoutTests.m b/iphone/layout/layoutTests/layoutTests.m index 8ddcd15898e..d7bb9c3c8bf 100644 --- a/iphone/layout/layoutTests/layoutTests.m +++ b/iphone/layout/layoutTests/layoutTests.m @@ -488,7 +488,6 @@ -(void)test_VerticalLayoutFillSizeLast [otherView setBackgroundColor:[UIColor darkGrayColor]]; [otherView setViewName:@"otherView"]; - [label setOnLayout:^(TiLayoutView *sender, CGRect rect) { XCTAssertEqualWithAccuracy(rect.origin.x, 10, 2); XCTAssertEqualWithAccuracy(rect.origin.y, 10, 2); @@ -781,8 +780,7 @@ -(void)test_ToolbarItems [toggle1 setOnLayout:^(TiLayoutView *sender, CGRect rect) { XCTAssertEqualWithAccuracy(rect.size.width, 49, 0); XCTAssertEqualWithAccuracy(rect.size.height, 31, 0); - - XCTAssertEqualWithAccuracy(rect.origin.x, 16, 1); + XCTAssertEqualWithAccuracy(rect.origin.x, 20, 1); XCTAssertEqualWithAccuracy(rect.origin.y, 6.5, 1); done1 = YES; @@ -790,8 +788,7 @@ -(void)test_ToolbarItems [toggle2 setOnLayout:^(TiLayoutView *sender, CGRect rect) { XCTAssertEqualWithAccuracy(rect.size.width, 49, 0); XCTAssertEqualWithAccuracy(rect.size.height, 31, 0); - - XCTAssertEqualWithAccuracy(rect.origin.x, 75, 1); + XCTAssertEqualWithAccuracy(rect.origin.x, 79, 1); XCTAssertEqualWithAccuracy(rect.origin.y, 6.5, 1); done2 = YES; @@ -799,8 +796,7 @@ -(void)test_ToolbarItems [toggle3 setOnLayout:^(TiLayoutView *sender, CGRect rect) { XCTAssertEqualWithAccuracy(rect.size.width, 49, 0); XCTAssertEqualWithAccuracy(rect.size.height, 31, 0); - - XCTAssertEqualWithAccuracy(rect.origin.x, 134, 1); + XCTAssertEqualWithAccuracy(rect.origin.x, 138, 1); XCTAssertEqualWithAccuracy(rect.origin.y, 6.5, 1); done3 = YES; @@ -809,6 +805,111 @@ -(void)test_ToolbarItems [toolbar setItems:@[toggle1,toggle2,toggle3]]; WAIT_FOR( (done1 && done2 && done3) ); +} + +-(void)test_CalculateHeight_AbsoluteLayout_BeforeAddingToSuperview +{ + __block BOOL done = NO; + // Parent + TiLayoutView* parentView = [[TiLayoutView alloc] init]; + [parentView setWidth:@300]; + [parentView setHeight:@500]; + [parentView setBackgroundColor:[UIColor lightGrayColor]]; + + // Test begins + TiLayoutView* view = [[TiLayoutView alloc] init]; + [view setWidth:@"SIZE"]; + [view setHeight:@"SIZE"]; + [view setBackgroundColor:[UIColor redColor]]; + + TiLabel* label = [[TiLabel alloc] init]; + [label setText:@"Bacon ipsum dolor amet jowl boudin prosciutto capicola, tail ribeye flank. Pork belly chicken meatloaf picanha chuck frankfurter pig filet mignon jowl ham"]; + + [view addSubview:label]; + CGFloat calculatedHeight = [view heightIfWidthWere:300]; + + [view setOnLayout:^(TiLayoutView *sender, CGRect rect) { + CGFloat realHeight = rect.size.height; + + XCTAssertEqual(realHeight, calculatedHeight); + XCTAssertEqual(realHeight, 101.500000); + done = YES; + }]; + + [parentView addSubview:view]; + [myView addSubview:parentView]; + WAIT_FOR(done); + +} +-(void)test_CalculateHeight_AbsoluteLayout_AfterAddingToSuperview +{ + __block BOOL done = NO; + // Parent + TiLayoutView* parentView = [[TiLayoutView alloc] init]; + [parentView setWidth:@300]; + [parentView setHeight:@500]; + [parentView setBackgroundColor:[UIColor lightGrayColor]]; + + // Test begins + TiLayoutView* view = [[TiLayoutView alloc] init]; + [view setWidth:@"SIZE"]; + [view setHeight:@"SIZE"]; + [view setBackgroundColor:[UIColor redColor]]; + + TiLabel* label = [[TiLabel alloc] init]; + [label setText:@"Bacon ipsum dolor amet jowl boudin prosciutto capicola, tail ribeye flank. Pork belly chicken meatloaf picanha chuck frankfurter pig filet mignon jowl ham"]; + + [view addSubview:label]; + + [parentView addSubview:view]; + [myView addSubview:parentView]; + + CGFloat calculatedHeight = [view heightIfWidthWere:300]; + + [view setOnLayout:^(TiLayoutView *sender, CGRect rect) { + CGFloat realHeight = rect.size.height; + XCTAssertEqual(realHeight, calculatedHeight); + XCTAssertEqual(realHeight, 101.500000); + done = YES; + }]; + + WAIT_FOR(done); +} + +-(void)test_CalculateHeight_VerticalLayout +{ + __block BOOL done = NO; + TiLayoutView* parentView = [[TiLayoutView alloc] init]; + [parentView setWidth:@300]; + [parentView setHeight:@"SIZE"]; + [parentView setLayout_:@"vertical"]; + [parentView setBackgroundColor:[UIColor lightGrayColor]]; + + TiLayoutView* view = [[TiLayoutView alloc] init]; + [view setWidth:@"100"]; + [view setHeight:@"100"]; + [view setViewName:@"this_view"]; + [view setBackgroundColor:[UIColor redColor]]; + + TiLabel* label = [[TiLabel alloc] init]; + [label setText:@"Bacon ipsum dolor amet jowl boudin prosciutto capicola, tail ribeye flank. Pork belly chicken meatloaf picanha chuck frankfurter pig filet mignon jowl ham"]; + + [parentView addSubview:view]; + [parentView addSubview:label]; + + + CGFloat calculatedHeight = [parentView heightIfWidthWere:300]; + + [parentView setOnLayout:^(TiLayoutView *sender, CGRect rect) { + CGFloat realHeight = rect.size.height; + XCTAssertEqual(realHeight, calculatedHeight); + XCTAssertEqual(realHeight, 201.500000); + done = YES; + }]; + + [myView addSubview:parentView]; + + WAIT_FOR(done); } @end