2

I'm trying to add a shadow to a subview of a custom UITableViewCell. The subview serves as the visible background of the cell (the root view of the cell is clear).

The cell is laid out in the interface builder using a xib. The subview is laid out using autolayout constraints to the 4 edges of the cell.

For different width devices, the subview itself seems to autoresize fine. From stack overflow research I figured that I have to put my frame-dependent code in layoutSubviews method. I put my shadow drawing code there:

layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:subView.bounds cornerRadius:3.0]CGPath];

the shadow appears, but only to the size of the frame before it is resized, which after some debugging seems to be the view frame rectangle, a modifiable constant in interface builder (that is actually grayed out for autolayout view controllers :@).

From further debugging it appears that the subview frame is never updated, even though the cell view frame is.(this was performed by printing the width of the view's frame in layoutSubviews)

 layoutSubviewscalled, parent view frame size: 180 subview frame size: 148.000000
 layoutSubviewscalled, parent view frame size: 375 subview frame size: 148.000000

is this a bug? Am I missing something? What can I do to reprimand this?

Sameer J
  • 226
  • 3
  • 13

1 Answers1

0

As seen in this answer, you should call [self setNeedsLayout] before inserting the UIBezierPath as the shadowPath.

Community
  • 1
  • 1
GBF_Gabriel
  • 2,636
  • 2
  • 17
  • 15
  • thank you very much for replying. I went over this answer before but not patiently enough. This seems to work but only on the 3rd layoutSubviews call. Any ideas how I can only draw the shadow after the view updates? – Sameer J Oct 07 '14 at 17:14
  • @SameerJJ turns out I was having a problem with auto layout as well, in my case, now I call `self.layoutIfNeeded()` before initializing my custom UIScrollView, at my View Controller (as Apple Documentation states: _Using the view that receives the message as the root view, this method lays out the view subtree starting at the root._) You tried to `[self setNeedsLayout]` followed by `[self layoutIfNeeded]`? – GBF_Gabriel Oct 07 '14 at 17:48
  • yea I did try that but it yielded to be unsuccessful. Even with the layout finally returning the correct frame size it was still overall quite buggy. I really think Apple needs to step up here and make autolayout less of pain to work with. I've gone back to programmatic view layouts for this cell. Thanks for the help – Sameer J Oct 08 '14 at 21:14
  • I don't think AutoLayout is that horrible to work with (perhaps because I always assume I don't know enought whenever I face a problem). I'll let another help to random searchers, although not applicable to Sameer's problem: be sure to start the drawning and initialization of subviews when `frame` is needed on `viewDidAppear()` instead of `viewDidLoad()`, this way you'll npbe sure that all the frames are correct. – GBF_Gabriel Oct 09 '14 at 22:22