最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift子页面的frame的尺寸获取不到

Swift crifan 3161浏览 0评论

swift中,已经通过init初始化子页面,

之后去通过

subView.frame.width

结果无法获得frame的尺寸,width和height都是0

swift subview frame size 0

swift uiview frame size 0

 

swift – iOS – Subview frame size inherited from parent view – Stack Overflow

objective c – Unable to set frame correctly before viewDidAppear – Stack Overflow

viewDidLoad is called when the class is loaded however no ui elements have been initialised and therefore any attempt to reference them will be overwritten or unavaliable during the initialisation process which happens between the viewDidLoad and viewDidAppear calls. Once all ui element have been initalised and drawn viewDidAppear is called.

viewDidLoad – Called after the controller’s view is loaded into memory

At this point the view isn’t within the view hierarchy.

viewWillAppear – Notifies the view controller that its view is about to be added to a view hierarchy.

Again, the view is yet to be added to the view hierarchy.

viewDidAppear – Notifies the view controller that its view was added to a view hierarchy.

 

swift get sub view frame size

swift UIView frame size

 

iphone – When is layoutSubviews called? – Stack Overflow

    • init does not cause layoutSubviews to be called (duh)
    • addSubview: causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target
    • view setFrame intelligently calls layoutSubviews on the view having its frame set only if the size parameter of the frame is different
    • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and its superview
    • rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
    • Resizing a view will call layoutSubviews on its superview

 

感觉有可能是:

之前虽然调用了init,其中(或是只之后才)调用addSubview,

-》所以当时父view中获取子view的frame,是没有得到当前的真正的frame的?

 

抽空再去试试:

setFrame(newFrame)

(而不是之前的 subView.frame = newFrame)

 

【后记】

后来确定了:

对于ViewController来说,init时候,就是获取不到父view的frame的

-》当viewDidLoad时,就可以了。

转载请注明:在路上 » 【已解决】swift子页面的frame的尺寸获取不到

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.216 seconds, using 22.08MB memory