输出指的是在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 } } }更多推荐
发布评论