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

后端 未结 6 899
臣服心动
臣服心动 2020-12-03 04:23

I tested my app with iOS 10 Beta 7 and Xcode 8 beta and everything worked fine. However just a few minutes ago I installed the now available GM releases of both and faced a

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 04:57

    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 *)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.

提交回复
热议问题