在viewDidAppear之后,无法获得表视图或表视图头的宽度(Cant get width of table view or table view header until after viewD

编程入门 行业动态 更新时间:2024-10-10 05:22:46
在viewDidAppear之后,无法获得表视图或表视图头的宽度(Cant get width of table view or table view header until after viewDidAppear) func setTableHeader(){ let headerWidth = Double(tableHeader.frame.width) // also tried let headerWidth = Double(tableView.frame.width) print("header width is \(headerWidth)") } func viewWillAppear(){ output --> 351 setTableHeader() //also tried in View Did Appear } func viewDidDisappear(){ output --> 296 setTableHeader() }

输出指的是在Iphone SE上运行时发生的情况。 我使用Iphone 6显示器在Storyboard中设计了带有约束的tableview和表头。 我能够通过改变setTableHeader来反映view.frame.width - (故事板中的左右约束)来获得我想要的结果,但是我想知道为什么我不能让它工作,以及不那么hacky的工作是什么? 另外,我实际上在setTableHeader中设置表头的子视图的维度,但这些子视图没有约束(编程或故事板),如果相关的话。

谢谢。

func setTableHeader(){ let headerWidth = Double(tableHeader.frame.width) // also tried let headerWidth = Double(tableView.frame.width) print("header width is \(headerWidth)") } func viewWillAppear(){ output --> 351 setTableHeader() //also tried in View Did Appear } func viewDidDisappear(){ output --> 296 setTableHeader() }

The outputs refer to what occurs when run on Iphone SE. I designed my tableview and table header with constraints in Storyboard with the Iphone 6 display. I was able to get the result I wanted by changing setTableHeader to reflect view.frame.width - (left and right constraints from storyboard), however I was wondering why I couldnt get this to work, and what the less hacky work around would be? Also I am in actuality setting the dimensions of table header's subviews inside setTableHeader, but these subviews have no constraints (programatically nor in storyboard), in case that is relevant.

Thank you.

最满意答案

试试这个,解决方法是覆盖UIViewController()。viewDidLayoutSubviews(),根据它的约束获取标题视图的正确大小,在标题上设置框架,并将其重置为表头视图

override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() // Dynamic sizing for the header view if let headerView = tableView.tableHeaderView { let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height var headerFrame = headerView.frame // If we don't have this check, viewDidLayoutSubviews() will get // repeatedly, causing the app to hang. if height != headerFrame.size.height { headerFrame.size.height = height headerView.frame = headerFrame tableView.tableHeaderView = headerView } } }

Try this , The solution was to override UIViewController().viewDidLayoutSubviews(), get the proper size of the header view based on it’s constraints, set the frame on the header, and reset it as the table header view

override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() // Dynamic sizing for the header view if let headerView = tableView.tableHeaderView { let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height var headerFrame = headerView.frame // If we don't have this check, viewDidLayoutSubviews() will get // repeatedly, causing the app to hang. if height != headerFrame.size.height { headerFrame.size.height = height headerView.frame = headerFrame tableView.tableHeaderView = headerView } } }

更多推荐

本文发布于:2023-08-03 08:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1386284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   宽度   viewDidAppear   width   header

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!