NG_Loc NG_Loc - 1 month ago 18
iOS Question

cornerRadius causes long load time in iOS 10

I have a problem with my App. I have a View with 25 Buttons with changed cornerRadius. Since iOS 10 it takes about 5-6 seconds till this View loads.
Here is the relevant code:

- (void)setupTiles {
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
}
[self colorTilesWithArray:currentTileColors];
}


When i remove the following lines, is loads the view instantly:

[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;


The Buttons are grouped in an outlet collection, in case that's necessary to know.

Does anybody have a solution?
Thanks in advance!

Nico

Image 1: This takes about 5-6 seconds

Image 2: This is the View that will be loaded

TileButton.h-File:

@interface TileButton : UIButton

@property (nonatomic) int colorMode;

+ (UIColor *)blue;
+ (UIColor *)red;
+ (UIColor *)green;
+ (UIColor *)yellow;
+(UIColor *)colorForColorCode:(int)colorCode;
-(int)colorMode;

@end


TileButton.m-File:

@implementation TileButton
- (instancetype)init {
self = [super init];
if (self) {
self.layer.cornerRadius = self.frame.size.width*0.3;
}
return self;
}

+(UIColor *)blue {
return [UIColor colorWithRed:41.0/255.0 green:161.0/255.0 blue:255.0/255.0 alpha:1];
}

+(UIColor *)red {
return [UIColor colorWithRed:255.0/255.0 green:71.0/255.0 blue:109.0/255.0 alpha:1];
}

+(UIColor *)green {
return [UIColor colorWithRed:0.0/255.0 green:185.0/255.0 blue:30.0/255.0 alpha:1];
}

+(UIColor *)yellow {
return [UIColor colorWithRed:255.0/255.0 green:198.0/255.0 blue:26.0/255.0 alpha:1];
}

+(UIColor *)colorForColorCode:(int)colorCode {
switch (colorCode) {
case 1:
return [TileButton blue];
break;
case 2:
return [TileButton red];
break;
case 3:
return [TileButton green];
break;
case 4:
return [TileButton yellow];
break;
default:
return [UIColor blackColor];
break;
}
}

-(int)colorMode {
if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton blue].CGColor))
return 1;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton red].CGColor))
return 2;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton green].CGColor))
return 3;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton yellow].CGColor))
return 4;
else
return -1;
}

@end

Answer

Since you are creating the buttons in Interface Builder you will need to set the cornerRadius in the awakeFromNib method. It should look something like this:

@implementation TileButton
-(void) awakeFromNib {
     [super awakeFromNib];
     self.layer.cornerRadius = self.frame.size.width*0.3;
}

https://developer.apple.com/reference/objectivec/nsobject/1402907-awakefromnib

Comments