Приложение, над которым я работаю, имеет настроенный класс UIButton, который должен создавать закругленную кнопку размера в зависимости от его заголовка.
С момента обновления до iOS 13 что-то происходит странное:
при первом появлении вида, содержащего эти кнопки, все работает хорошо. Затем я переключаюсь на другой контроллер представления, затем go возвращаюсь к представлению с помощью кнопок, и здесь я получаю бесконечное значение l oop для метода -layoutsubviews.
Это настраиваемый класс:
#import "RoundedButton.h"
@interface RoundedButton()
@property (nonatomic, retain) NSString *text;
@end
@implementation RoundedButton
-(void)awakeFromNib{
[super awakeFromNib];
_text = self.currentTitle;
[super setTitle:@"" forState:UIControlStateNormal];
}
-(void)layoutSubviews{
[super layoutSubviews];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
NSDictionary <NSString *, id>*attr = @{ NSFontAttributeName:self.titleLabel.font, NSParagraphStyleAttributeName:style };
CGRect r = self.bounds;
r.size = [_text sizeWithAttributes:attr];
r.size.width += 20;
r.size.height += 20;
r.size.width = MAX( r.size.width, 200 );
r.size.height = MAX( r.size.height, 40 );
[self setBounds:r];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
if ( ![self.backgroundColor isEqual:[UIColor clearColor]] ){
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:_cornerRounded];
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
[bezierPath fill];
//CGContextSetBlendMode(context, kCGBlendModeCopy);
//CGContextFillRect(context, self.bounds);
CGContextSetBlendMode(context, kCGBlendModeClear);
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
NSDictionary <NSString *, id>*attr = @{ NSFontAttributeName:self.titleLabel.font, NSParagraphStyleAttributeName:style };
CGSize szText = [_text sizeWithAttributes:attr];
CGRect r = self.bounds;
r.origin.y += (r.size.height - szText.height)/2;
[_text drawInRect:r withAttributes:attr];
}
}
-(void)setEnabled:(BOOL)enabled{
[super setEnabled:enabled];
if ( !enabled ){
self.alpha = 0.8;
}
else{
self.alpha = 1.0;
}
}
-(void)setTitle:(NSString *)title forState:(UIControlState)state{
_text = title;
[self setNeedsDisplay];
}
-(void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state{
_text = title.string;
[self setNeedsDisplay];
}
@end