Automatic change layout of a view
Is there a way to automatically have an element (eg WebView) resize when a nearby element (UIImageView) is hidden?
I have a normal UIView with a WebView in it开发者_Python百科 showing HTML, but in some cases there is a need for an image above that. So I put the image above the WebView, but would like the webview to also take the space of the ImageView when this one is not present, otherwise there is a white bar.
Another option is to place it over the Webview, but I don't think there is anyway to have the text flow around the image.
Anybody any suggestions for a tidy solutions? Thanks a million in advance!
A handy trick I learned is to rely on CGRectDivide()
--of course you will have to write your own layoutSubviews
method, but it's not too difficult:
-(void)layoutSubviews
{
CGRect bounds = self.bounds ;
if ( !self.imageView.hidden )
{
CGRect slice ;
CGRectDivide( bounds, & slice, & bounds, IMAGE_VIEW_HEIGHT, CGRectMinYEdge ) ;
self.imageView.frame = slice ;
}
// bounds here contains the remaining layout space with or without the image view
self.webView.frame = bounds ;
}
Why is it such a problem to just resize your WebView in the same method that shows or hides the image?
By the way, Cocoa Autolayout was introduced for OSX Lion. iOS 6 is under NDA at the moment, but if you have a developer account, go have a look, maybe there is something like it there.
Check out CSLinearLayoutView. It tries to mimick Android's LinearLayout and RelativeLayout, and should be able to simplify dynamic resizing/moving of views.
精彩评论