Skip to content

Commit

Permalink
[AutoLayout] more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pec1985 committed Mar 1, 2016
1 parent 93170ec commit 7d0343b
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 125 deletions.
80 changes: 20 additions & 60 deletions iphone/Classes/Layout/TiLayoutView.m
Expand Up @@ -5,47 +5,6 @@
* Please see the LICENSE included with this distribution for details.
*/

#import <objc/runtime.h>

@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"
Expand Down Expand Up @@ -492,18 +451,23 @@ -(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];

[dummyView layoutIfNeeded];

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

[self removeFromSuperview];


_loaded = NO;
if (parent != nil) {
[parent addSubview:self];
TiLayoutView* viewToUpdate = nil;
Expand Down Expand Up @@ -602,7 +566,7 @@ -(void)layoutChildren

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

Expand All @@ -628,6 +592,7 @@ -(void)didMoveToSuperview
[self updateWidthAndHeight];
[self layoutChildren];
}

[super didMoveToSuperview];
}

Expand Down Expand Up @@ -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)];
}
}
Expand Down Expand Up @@ -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)];
}
Expand Down Expand Up @@ -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)];
}
Expand Down
14 changes: 13 additions & 1 deletion iphone/Classes/TiUITableView.m
Expand Up @@ -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"
Expand All @@ -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]];
Expand Down
6 changes: 5 additions & 1 deletion iphone/Classes/TiUITableViewRowProxy.h
Expand Up @@ -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;
Expand Down
43 changes: 35 additions & 8 deletions iphone/Classes/TiUITableViewRowProxy.m
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
{
Expand All @@ -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;
Expand Down Expand Up @@ -282,6 +301,7 @@ -(CGFloat)sizeWidthForDecorations:(CGFloat)oldWidth forceResizing:(BOOL)force
return width;
}


-(CGFloat)rowHeight:(CGFloat)width
{
if (TiDimensionIsDip(height))
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions iphone/Classes/TiUIView.m
Expand Up @@ -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];
Expand Down
16 changes: 16 additions & 0 deletions iphone/layout/Classes/TiTableView.h
Expand Up @@ -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<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSArray* tableData;

@end

0 comments on commit 7d0343b

Please sign in to comment.