clipsToBounds causes UIImage to not display in iOS10 & XCode 8

后端 未结 10 1549
南方客
南方客 2020-12-08 10:28

I switched my project over to new beta versions of iOS 10 and XCode 8. In all three areas of my app where I use:

imageView.layer.cornerRadius = imageView.fra         


        
相关标签:
10条回答
  • 2020-12-08 10:55

    Using obj-c, moving the syntax from viewDidLoad to viewDidLayoutSubviews worked for me.

    0 讨论(0)
  • 2020-12-08 11:00

    This problem also happened to me.

    I moved these code imageView.layer.cornerRadius = imageView.frame.size.width/2 from - (void)awakeFromNib to - (void)drawRect:(CGRect)rect and this problem went away.

    My imageView is sized by autolayout. I think that height and width are not decided when awaking from nib on iOS 10.

    0 讨论(0)
  • 2020-12-08 11:00

    I just implemented corner rounding 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 I always have properly rounded corners. And I hadn't issues on upgrading to iOS10 and XCode8

    0 讨论(0)
  • 2020-12-08 11:02

    I had ImageView that is fully circular in shape. Below is code :

    //MARK:- Misc functions
    func setProfileImage() {
        imgProfile.layer.masksToBounds = false
        imgProfile.layer.cornerRadius = imgProfile.frame.size.height/2
        imgProfile.clipsToBounds = true
        imgProfile.contentMode = UIViewContentMode.ScaleToFill
    } 
    

    This works fine in iOS 9. However, in iOS 10 Xcode 8 the ImageView disappears. After debugging found that ClipsToBound is culprit.

    So placed the code in viewDidLayoutSubviews() resolved the issue.

    override func viewDidLayoutSubviews() {
        setProfileImage()
    }
    

    You can also use self.view.layoutIfNeeded()

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