简体   繁体   中英

iOS incorrect frame size at runtime

I've got an UIImageView inside a root view of a controller, and I've set it to be 90% of the screen width and given it an aspect ratio as constraints to set the dimensions.

In the code I'm trying to do something with respect to the size of the UIImageView at runtime, however when I get the frame.size.height or frame.size.width of the UIImageView they are clearly wrong and way too small.

At first I was accessing the size in viewDidLoad() , after which I found quite a few posts suggesting to do it either in viewWillLayoutSubviews() , viewDidLayoutSubviews() , or viewWillAppear() . Unfortunately I've tried all of those and none of these contexts seem to provide the right value. I suspect it may have something to do with auto layout but I'm not quite sure how to get around this. Any insight as to why this may be would be appreciated

viewDidLoad is too early. At this time, the views have the frames they were given in the storyboard. Ditto for viewWillAppear .

In viewWillLayoutSubviews , the view controller's top-level view has its correct frame, but its descendants do not.

In viewDidLayoutSubviews , the view controller's immediate subviews have their correct frames, but more distant descendants (“grandchildren” and so forth) don't.

If the image view is a direct subview of the view controller's view, then its frame is up to date in viewDidLayoutSubviews .

If the image view is a more distant descendant, then there is no method you can override in the view controller that will be called after the image view's frame has been updated but before the image view is visible on screen. Here are two options in this case:

  1. Create a custom subclass of UIView to be the superview of the image view. When the superview's layoutSubviews runs, after it calls super.layoutSubviews , the image view's frame is up to date.

  2. Create a hidden UIView that is a direct subview of the view controller's top-level view. Use constraints to make this hidden view's frame exactly match the image view's frame. Hidden views participate in layout, so when viewDidLayoutSubviews is called, this hidden view's frame is up to date, and is the same as the image view's frame will eventually be (except that the hidden view's frame is in the top-level view's geometry, which might be different than the geometry of the image view's superview).

if you try to get width or height in viewDidload() then height and width is you get wrong because in viewDidload method controller is not complete load it's loading so that time you getting it's size that is wrong.

if you need height and width in viewDidload() method so you can use

 DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
        //get height or width
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM