[英]Update tableView's headerView on device rotation
I have table view with a custom header view. 我有一个带有自定义标题视图的表视图。 In the header view I have a subview that is a UIImage.
在标题视图中,我有一个子视图,即UIImage。 On rotation, the header view and it's subviews don't update to where they should be.
旋转时,标题视图及其子视图不会更新到应有的位置。 Instead, they retain their positions until the user either presses a button, moves to a different page, etc. Essentially, their positions don't update until the user interacts with the app in some way.
而是保留它们的位置,直到用户按下按钮,移动到其他页面等为止。从本质上讲,直到用户以某种方式与应用程序交互时,它们的位置才会更新。
I am detecting device rotation with: 我正在通过以下方式检测设备旋转:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
// Update header views!
}
I can't find any sort of code that will update the header view and it's subviews though. 我找不到任何可以更新标题视图及其子视图的代码。 Any help appreciated.
任何帮助表示赞赏。
EDIT: Here is the code for my header view: 编辑:这是我的标题视图的代码:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, tableView.sectionHeaderHeight))
let headerColor = UIColor.blueColor()
headerView.backgroundColor = headerColor
headerView.tag = section
let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-45, height: 30)) as UILabel
headerString.text = "Title"
headerString.textColor = UIColor.whiteColor()
headerView.addSubview(headerString)
let headerTapped = UITapGestureRecognizer(target: self, action:"sectionHeaderTapped:")
headerView.addGestureRecognizer(headerTapped)
// Button in header
let gearImage = UIImage(named: "icons_button")
let width = CGFloat(45)
let rightInset = CGFloat(10)
let headerButton = UIButton(frame: CGRect(x: headerView.frame.maxX - 45, y: 7, width: width, height: width - rightInset))
headerButton.setImage(gearImage, forState: UIControlState.Normal)
headerButton.tag = section
headerButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: rightInset)
headerButton.addTarget(self, action: "userButtonPress:", forControlEvents: UIControlEvents.TouchUpInside)
headerView.addSubview(headerButton)
return headerView
}
在viewForHeaderInSection中添加类似UIVIEW的容器,然后在容器中添加类似label的childview,然后添加此行
label.autoresizingMask = .flexibleWidth
在您的旋转函数中:
self.view.layoutIfNeeded()
Although it seems like overkill, this works well: 尽管这看起来有些过分,但是效果很好:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[coordinator animateAlongsideTransition: ^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[_myTableView reloadData];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[_myTableView reloadData];
}];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.