I tried translating Apple's CollectionView-Simple demo app from Objective-C to Swift 1.2 using Xcode 6.3 and a fresh project. It's not working right. Specifically, the view does not size itself properly when rotating the device. It also has sizing issues in portrait mode that are different for iPhone 5s and iPad 2.
I've hunted through the storyboard for differences and can't find the issue. I don't know what the relevant code to post is, so I have the whole thing up on GitHub:
https://github.com/DavidSteuber/CollViewSwift
Does anyone see the problem and how to solve it?
The problem is with the autoresizingMask
in your Storyboard. I replaced your CollectionViewController with a fresh one, and it worked as expected. Looking at the source code for the two view controllers, only 1 difference jumped out:
Your collection view controller:
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
New collection view controller:
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
I'm not sure how this happened (perhaps you used an old Storyboard?), but you can fix it in one of three ways.
Control -click on Main.storyboard
in the Project Navigator and select Open As->Source Code
. Then change flexibleMaxX
to widthSizable
and change flexibleMaxY
to heightSizable
. Save the file, and then Control -click on Main.storyboard
in the Project Navigator and select Open As->Interface Builder - Storyboard
to put it back.
Instead, you could just recreate the Collection View Controller from scratch as I did.
Finally, you can also fix this problem in code. You can add this method to your your main ViewController
:
override func viewDidLoad() { super.viewDidLoad() self.collectionView!.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight }
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.