Resize UICollectionView after view transition - ios

I've built detail view in Interface Builder showing informations and photos about some object. Because lenght of informations and number of photos will vary, all is nested in UIScrollView.
Photos are shown in UICollectionView, but I need to always show all contained photos, so I disabled scrolling and dynamically changing Height constraint of UICollectionView by this function (called when finishing rendering cells):
func resizePhotosCollectionView() {
photosCollectionViewHeightConstraint.constant = photosCollectionView.collectionViewLayout.collectionViewContentSize()
}
It works great until this UICollectionView needs resize (typically by device orientation change). I am trying to use UIViewControllerTransitionCoordinator in function:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animateAlongsideTransition(nil) { context in
self.resizePhotosCollectionView()
}
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}
but result is jerky because Height constraint changed after transition is complete.
Is there any way how to automatically resize UICollectionView after view transition? If not, how to animate Height constraint change during transition?
Using Xcode 6.1 for target IOS 8.

If I recall correctly, you need to layout the view immediately.
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animateAlongsideTransition(nil) { context in
self.resizePhotosCollectionView()
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}

Have you tried wrapping your constraint update into a view animation?
photosCollectionViewHeightConstraint.constant =
photosCollectionView.collectionViewLayout.collectionViewContentSize()
photosCollectionView.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.3, animations: { () -> Void in
photosCollectionView.layoutIfNeeded()
})

Related

Swift viewWillTransition not called

I'm creating a full screen image gallery using a UICollectionView. When the user rotates the device, I perform updates to the UICollectionView within
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
I present this UIViewController modally and have a UICollectionView taking up the full screen. Within viewDidLoad, I create the flow layout as:
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
photosCollectionView.isPagingEnabled = true
photosCollectionView.setCollectionViewLayout(flowLayout, animated: true)
I also have the size as:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return photosCollectionView.frame.size
}
When I rotate my device, viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) is never called, which causes the UICollectionViewLayout to not update. While I rotate the device, I do get the message:
The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
I've read online that I can add:
self.automaticallyAdjustsScrollViewInsets = false
to the UIViewController, but that had no affect. There are no content or section insets with the UICollectionView.
I also have the super.viewWillTransition called within the function as well. Can anyone assist me on what could be causing this issue?
If you are just concern about the layout when the device rotate then please use:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
collectionView.layoutIfNeeded()
}
From apple docs:
public func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
This method is called when the view controller's view's size is
changed by its parent (i.e. for the root view controller when its
window rotates or is resized).
If you override this method, you should either call super to propagate the change to children or manually forward the change to
children.
I guess you might called this function on a parent of that view without calling super
A work around would also be to register for the device rotation:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
#objc func deviceOrientationDidChange(_ notification: Notification) {
let orientation = UIDevice.current.orientation
print(orientation)
}
I'd like to repost an answer which i left in another thread which describes the same issue.
People have already explained that you have to call super. I'd like to add a piece of information that might help people who would have faced what i faced.
Scenario: Parent -> Child (viewWillTransition not called in child)
If your view controller is a child view controller, then check if the parent view controller delegate is called and if super is called there. Otherwise it won't be propagated to the child view controller!
class ParentViewController: UIViewController {
func presentChild() {
let child = ChildViewController()
present(child, animated: false, compeltion: nil)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator) // If this line is missing your child will not get the delegate call in it's viewWillTransition
// Do something
}
}
class ChildViewController: UIViewController {
// This method will not get called if presented from parent view controller and super is not called inside the viewViewWillTransition available there.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
//Do something
}
}
P.S - This happened to me because the code for the parent was written by someone else and they forgot to call super.
I replaced
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
by
func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)
and it works for me.
This is how I fixed that:
Go to Project Target - General - Deployment Info
Tick Device Orientations the app supports
Also you need (change UIInterfaceOrientationMask.all for whatever is appropriate):
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
get {
return UIInterfaceOrientationMask.all;
}
}
for ViewController.
If your ViewController is a child ViewController and you have added it’s view to a parent ViewController’s view, do the following:
In parent ViewController where you added
self.view.addSubview(childViewController.view)
Also add
self.addChild(childViewController)
Maybe you need to set Supported interface orientations in Info.plist:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>

Property observer working for bounds but not frame

I'm working on this project and I have one scene on the Storyboard.
The scene is a TableViewController.
The table view have a custom prototype cell (linked to CustomCell.swift).
Inside the prototype cell there's a label and a custom UIView (linked to CustomView.swift). These elements have layout constraints relative to the contentView of the prototype cell.
Now, I want the stuff being drawn on my custom view to change when the size of the view changes, so that when the device rotates it is adjusted to that new cell width. Because of the constraints, the frame of CustomView will change when the CustomCell changes size, after the device is rotated. In order to detect this, I added two property observers to CustomView.swift:
override var frame: CGRect {
didSet {
print("Frame was set!")
updateDrawing()
}
}
override var bounds: CGRect {
didSet {
print("Bounds were set!")
updateDrawing()
}
}
When running the project, the second observer works fine when I rotate the device. The first observer does not. My question is why does the first observer not detect that the frame has changed?
.frame is computed from the .bounds and the .center of the view (and the transform), so it does not change. In order to react to rotation override this (starting from iOS8):
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (coordinator) -> Void in
// do your stuff here
// here the frame has the new size
}, completion: nil)
}

viewWillTransitionToSize called before all subviews have auto-laid-out

I have a View Controller with a view hierarchy within two nested views that are placed with autolayout constraints.
I then place in code some UIViews into one of those views but I don't want to use constraints for these as the user can drag them around.
If the device gets rotated, or the user splits the view using the slide over functionality of the iPad I want to reset the locations of these UIViews into their new place based on the automatic layout of the parent views:
I have a function in the questionView called resetAnswerPosition() that correctly positions all of the Views based on the position of the reference View after the auto-layout positioning has kicked in. QuestionView has stores a reference to the ReferenceView, to make this easier.
The documentation suggests using viewWillTransitionToSize & willTransitionToTraitCollection to run these changes which works for rotation, or for when the slide over changes the aspect ratio from landscape to portrait or back again. However on an iPad Pro when change the split from 2/3 to 1/2 the aspect ratio isn't changing that the resetAnswerTilesPositions is being called before the Reference View has been resized.
I have also tried to use the viewDidLayoutSubViews but again have the issue that this is run before the Reference view has been resized.
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (context) -> Void in
self.questionView.resetAnswerTilePositions()
},
completion: { (context) -> Void in
})
}
override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.questionView.resetAnswerTilePositions()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.questionAnswerView.resetAnswerTilePositions()
}
What function can I register either with the View Controller or the QuestionView which will only be called once all of the views have been laid out according to the updated constraints?
As always posting a question led me to the right answer.
In the Question View you can override the layoutSubviews routine which is really nice from a separation of concerns of point of view. Only the Question View then needs to know how to layout its own subviews.
override func layoutSubviews() {
super.layoutSubviews()
self.resetAnswerTilePositions()
}

How to get the size of a UICollectionView cell before rotation

I have a UICollectionView that has a width that is a percentage of it's parent. When it rotates I need to know what the size it is going to be, so I can invalidate the layout and redraw the collection view cells to be the new full width. I was using viewWillTransitionToSize:withTransitionCoordinator: method, but the size it returns is of the entire screen. Is there any easy way to get the new frame of a UICollectionView before the orientation change takes effect?
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
print(self.collectionView.frame) // old frame
coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in
}) { (UIViewControllerTransitionCoordinatorContext) -> Void in
print(self.collectionView.frame) // new frame
}
}

How can I get the height of a view after screen rotation in swift?

There is a view, which I add constraints in storyboard so it will change its size after screen rotation, then how can I get its height and width after screen rotation? I tried this in a rotation event function like this:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
println("h:\(view.bounds.size.height)")
println("w:\(view.frame.size.width)")
}
but it only give me the size before rotation happen. I would like to get something like:
"now is landscape, height is...width is..."
"now is portrait, height is ...width is..."
in a rotation event function
The function you are using says "Will transition", that means the transition is not completed, so you can not fetch the new size.
You need to use :
dispatch_async(dispatch_get_main_queue()) {
println("h:\(view.bounds.size.height)")
println("w:\(view.frame.size.width)")
}
This code will be executed after the rotation was completed (in the main queue) and the new sizes are available.
Regards.
Swift 3
crom87 answer updated for Swift 3:
override func viewWillTransition( to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator ) {
DispatchQueue.main.async() {
print( "h:\(self.view.bounds.size.height)" )
print( "w:\(self.view.frame.size.width)" )
}
}

Resources