在我的 iPhone 7 plus 上它工作正常,但在模拟器 iPhone 5 SE上,只有在我切换选项卡或滚动视图后才会UIView
填满屏幕。
我正在使用XLPagerTabStrip (9.0.0) 来获取两个页面。
我添加了一个UIView
蓝色背景用于调试目的,但我想要实现的是一个带有书籍的列表。
我希望UICollectionView
项目填满整个屏幕,这是我的实现:
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
print(String(format: "width is: %f", collectionView.bounds.width))
return CGSize(width: collectionView.bounds.width, height: CGFloat(90))
}
这是我在控制台中得到的:width is: 226.000000
,但是在滚动或切换选项卡后,我得到以下结果:width is: 320.000000
我真的不知道发生了什么,AutoLayout 约束是通过故事板从上到下设置的,从前到后填充整个屏幕:
请注意:在故事板中,我将蓝色设置为,UIView
因为我想知道宽度错误来自哪里。(我猜它来自其他地方,而不是来自UICollectionView
,因为如您所见,UIView
它不会水平填充整个屏幕)。
我添加了一些调试日志来查看View Controllers
.
[ParentViewController]: viewControllers()
[ChildViewController]: viewDidLoad()
[ParentViewController]: viewDidLoad()
[ChildViewController]: viewWillAppear()
[ParentViewController]: viewWillAppear()
[ParentViewController]: viewDidLayoutSubviews()
[ChildViewController]: viewDidLayoutSubviews()
[ChildViewController]: viewDidLayoutSubviews()
[ChildViewController]: viewDidAppear()
[ParentViewController]: viewDidAppear()
(其中:ParentViewController
是 main View Controller
,其中包含两个子页面View Controllers
)
我不知道为什么[ParentViewController]: viewControllers()
之前调用了viewDidLoad()
。也许错误来自这里?
这是我的实现:
class ParentViewController: ButtonBarPagerTabStripViewController {
override func viewDidLoad() {
configureButtonBar()
super.viewDidLoad()
}
private func configureButtonBar() {
// set the button bar style (color, font size, green bar size, paddings, etc)
}
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
let child1 = UIStoryboard.init(name: "ParentStoryboard", bundle: nil).instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController
child1.pageTitle = "TAB 1"
child1.entries = KData.shared().books
let child2 = UIStoryboard.init(name: "ParentStoryboard", bundle: nil).instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController
child2.pageTitle = "TAB 2"
child2.entries = KData.shared().booksWithAuthors
return [child1, child2]
}
}
和ChildViewController
实施:
class ChildViewController: UIViewController, IndicatorInfoProvider, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var pageTitle: String = ""
var entries: [Book]? = nil
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
print(String(format: "width is: %f", collectionView.bounds.width))
return CGSize(width: collectionView.bounds.width, height: CGFloat(90))
}
}