Custom UIView's drawRect never called if initial frame is CGRectZero

后端 未结 2 2036
后悔当初
后悔当初 2021-02-13 21:14

I\'ve got a pretty simple custom subclass of UIView:

#import \"BarView.h\"
#import 

@implementation BarView

@synthesize barColor         


        
相关标签:
2条回答
  • 2021-02-13 22:12

    In Swift 5

    override init(frame: CGRect) {
          super.init(frame: frame)
    
          contentMode = UIView.ContentMode.redraw
    
    0 讨论(0)
  • 2021-02-13 22:15

    A quick look at the Apple docs says this

    Changing the frame rectangle automatically redisplays the view without calling its drawRect: method. If you want UIKit to call the drawRect: method when the frame rectangle changes, set the contentMode property to UIViewContentModeRedraw.

    This is in the documentation for the frame property:
    https://developer.apple.com/documentation/uikit/uiview/1622621-frame?language=objc

    If you want the view to redraw, just call setNeedsDisplay on it and you should be fine (or, as the docs say, you can set it to UIViewContentModeRedraw, but it is up to you)

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