I have table view with a custom header view. In the header view I have a subview that is a 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];
}];
}
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.