我有一个带有自定义标题视图的表视图。在标题视图中,我有一个UIImage的子视图。在轮播时,标题视图及其子视图不会更新到应该的位置。相反,他们保留他们的位置,直到用户按下按钮,移动到不同的页面等。基本上,他们的位置不会更新,直到用户以某种方式与应用程序交互。
我正在使用以下方法检测设备旋转:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
// Update header views!
}
我无法找到任何可以更新标题视图及其子视图的代码。任何帮助表示赞赏。
编辑:这是我的标题视图的代码:
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
}
答案 0 :(得分:1)
添加一个像UIVIEW的容器 然后在容器中添加类似标签的子视图 添加此行
label.autoresizingMask = .flexibleWidth
答案 1 :(得分:0)
:
.header {
background-image: url('images/image1.png');
background-size: cover;
background-repeat: no-repeat;
}
答案 2 :(得分:0)
虽然看起来有点矫枉过正,但效果还不错:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[coordinator animateAlongsideTransition: ^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[_myTableView reloadData];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[_myTableView reloadData];
}];
}