3

I have in a controller:

- (void)viewDidLoad {
   [super viewDidLoad];

   // Add a scroolView
   self.scroolViewDay.scrollEnabled = YES;
   // Compute the content Size of the TableDays
   self.scroolViewDay.contentSize = CGSizeMake(self.scroolViewDay.frame.size.width, 
                                               80 * 48); // TO MODIFY!
   [self.scroolViewDay addSubview:self.tableDays];
   [self.tableDays setNeedsDisplay];
}

The controller has a XIB where the UIScrollView is into. The custom view TableDays has a custom drawRect which is never called:

- (void)drawRect:(CGRect)rect {
    NSLog(@"sono in drawRect");
}

Why?

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
gdm
  • 7,647
  • 3
  • 41
  • 71
  • 1
    The solution is to init the frame. See this answer: http://stackoverflow.com/questions/6271681/simple-uiview-drawrect-not-being-called?rq=1 – gdm Jul 16 '13 at 09:14

2 Answers2

1
-(void) setNeedsDisplay {
    [self.subviews makeObjectsPerformSelector:@selector(setNeedsDisplay)];
    [super setNeedsDisplay];
}

Add this code, and just override setNeedsDisplay method in your main view and I hope that you know that all of your subviews should be redrawn.

iPatel
  • 46,010
  • 16
  • 115
  • 137
0

Before adding it programmatically, add it from the storyboard with the correct constraints and see if it getting called. In my case, there was a problem with the constraints I'm adding to this custom view.

This is the code with the problem,

    let header = HeaderBackgroundView(frame: view.bounds)
    scrollView.addSubview(header)

    header.snp.makeConstraints { make in
      make.leading.equalTo(scrollView.snp.leading)
      make.trailing.equalTo(scrollView.snp.trailing)
      make.top.equalTo(scrollView.snp.top)
      make.height.equalTo(200)
    }

i fixed it by adding the center x constraint:

    header.snp.makeConstraints { make in
      make.leading.equalTo(scrollView.snp.leading)
      make.trailing.equalTo(scrollView.snp.trailing)
      make.top.equalTo(scrollView.snp.top)
      make.centerX.equalTo(scrollView.snp.centerX) // this line.
      make.height.equalTo(200)
    }
Abedalkareem Omreyh
  • 2,180
  • 1
  • 16
  • 20