iOS 10 GM with xcode 8 GM causes views to disappear due to roundedCorners & clipsToBounds

妖精的绣舞 提交于 2019-11-27 13:03:19
Pranoy C

I am not sure if this is a new requirement, but I solved this by adding [self layoutIfNeeded]; before doing any cornerRadius stuff. So my new custom awakeFromNib looks like this:

- (void)awakeFromNib {
    [super awakeFromNib];
    [self layoutIfNeeded];
    self.tag2label.layer.cornerRadius=self.tag2label.frame.size.height/2;
    self.tag2label.clipsToBounds=YES;
}

Now they all appear fine.

To fix invisible views with cornerRadius=height/2 create category UIView+LayoutFix

In file UIView+LayoutFix.m add code:

- (void)awakeFromNib {
    [super awakeFromNib];
    [self layoutIfNeeded];
}

add category to YourProject.PCH file.

It will works only if you used [super awakeFromNib] in your views :

MyView.m

- (void)awakeFromNib {
    [super awakeFromNib];
    ...
}

cornerRadius itself works just fine but the size on the frame is reported incorrectly. which is why layoutIfNeeded fixes the issue.

I have faced the same issue on moving to TVOS 10. Removing auto layout constraints and using the new Autoresizing settings in storyboards solved it for me.

My observation is that iOS 10 / TVOS 10 is not laying out auto layout based views before calling awakeFromNib, but is laying out views using autoresizing masks before calling the same method.

You could create subclass of your view like this:

@implementation RoundImageView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = MIN(self.bounds.size.height, self.bounds.size.width)/2;
        [self addObserver:self
               forKeyPath:@"bounds"
                  options:NSKeyValueObservingOptionNew
                  context:(__bridge void * _Nullable)(self)];
    }
    return self;
}

-(void)dealloc
{
    [self removeObserver:self
              forKeyPath:@"bounds"
                 context:(__bridge void * _Nullable)(self)];
}

-(void)observeValueForKeyPath:(NSString *)keyPath
                     ofObject:(id)object
                       change:(NSDictionary<NSString *,id> *)change
                      context:(void *)context
{
    if(context == (__bridge void * _Nullable)(self) && object == self && [keyPath isEqualToString:@"bounds"])
    {
        self.layer.cornerRadius = MIN(self.bounds.size.height, self.bounds.size.width)/2;
    }
}

@end

so you'll always have properly rounded corners.

I use this approach and hadn't issues upgrading to Xcode8 and iOS10.

You can also see the view in the debug view hierarchy, but cannot see it in the app.

You have to call layoutIfNeeded on the affected masked/clipped view.

(E.g. If you have a UIImageView and you do masksToBounds on its layer and you cannot see the view in the app etc.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!