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

后端 未结 6 876
臣服心动
臣服心动 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<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.

    0 讨论(0)
  • 2020-12-03 04:58

    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.

    0 讨论(0)
  • 2020-12-03 05:01

    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.

    0 讨论(0)
  • 2020-12-03 05:04

    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];
        ...
    }
    
    0 讨论(0)
  • 2020-12-03 05:06

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

    0 讨论(0)
  • 2020-12-03 05:13

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

    0 讨论(0)
提交回复
热议问题