ios - What conditions can prevent layoutSubviews from being called after setNeedsLayout? -
the problem
in custom view of mine (a uiscrollview subclass) calling setneedslayout in response "reload data" event (triggered external source). of time works correctly , layoutsubviews called when next view layout cycle occurs. sometimes, however, layoutsubviews not called! until living "certain knowledge" setneedslayout always triggers layoutsubviews. apparently wrong. tried calling layoutifneeded after setneedslayout, still no success.
the question
obviously, solve particular problem. on other hand, improve understanding of view layout process on ios, formulating question in general way: know of conditions can prevent layoutsubviews being called after setneedslayout has been called? answers focus on uiscrollview quite welcome, since having trouble.
problem context
i on ios 7.1, using xcode 5.1.1. notes on implementation of custom scroll view:
- the scroll view has single container view of type
uiviewsame size scroll view content size - the scroll view's
layoutsubviewsimplementation places custom subviews container view manually calculating subviews' frames - the custom subviews' implementation uses auto layout
here how reloaddata implementation looks like:
- (void) reloaddata { // iterates through internal array holds subviews, // empties array. subviews deallocated @ // point. [self removeallsubviewsfromcontainerview]; self.contentoffset = cgpointmake(0, 0); // verified content size greater // cgsizezero (both width , height) cgfloat contentwidth = [self calculatenewcontentwidth]; self.contentsize = cgsizemake(contentwidth, self.frame.size.height); // here problem: triggers layoutsubviews, // not. [self setneedslayout]; // adding following line debugging purposes not // help, making clear setneedslayout has no effect. // [self layoutifneeded]; } last not least, here observations made:
layoutsubviewscalled expected if content offset or content size change.layoutsubviewsnot called in particular case if these 2 values don't change. first assumed observation general working principle ofuiscrollview, i.e.layoutsubviewsnot calleduiscrollviewunless content offset or content size change. however, when wrote couple of minimal projects failed reproduce assumed "general" behaviour - in projectslayoutsubviewscalled expected, regardless of whether content offset or content size changed or not.layoutsubviewscalled expected if scroll view displays dummyuilabelinstances instead of own custom subviewslayoutsubviewscalled expected if use own custom subviews, replace auto layout in custom subviews' implementation manual frame calculations
the last 2 observations have led me believe auto layout somehow involved, have no idea how affect how setneedslayout works.
Comments
Post a Comment