Using Swift 5, I am creating gradient backgrounds for the views in a UIPageControl:
var pageControl = UIPageControl()
func configurePageControl() {
self.view.applyGradient(colors: [UIColor.HL.PurpleStart,UIColor.HL.PurpleEnd])
}
Works great...all the pages have this purple gradient background. But when you rotate a device, the gradient rotates but keeps it's original size which is the device frame size in the previous orientation.
So I just want to redraw the gradient when the device is rotated. I use the following method which successfully executes after the device rotation is complete:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil, completion: { _ in
self.view.applyGradient(colors: [UIColor.HL.PurpleStart,UIColor.HL.PurpleEnd])
})
}
But this adds a new gradient background underneath the existing one. self.view.applyGradient() is a more current method of applying gradient backgrounds than other examples on SO, so the other solutions don't seem to apply. So how do you remove/replace the existing gradient when using self.view.applyGradient()?
Related
I have a UIView with a drawing on it that is composed of CALayers being created from a hardware device then me generating UIBezierPaths and added like so:
self.layer.addSublayer(currentLayer)
If my drawing is done after a device rotation, let's say, 90˚, the image is appropriate. However, after rotating back, the image is skewed and doesn't appear as it should. In essence, I want to redraw my CALayer with a new frame every time the device is rotated.
I've tried a few things, for starters, I tried rotating the UIView itself. Then I tried duplicating the layer that was being drawn on and rotating it, then removing all sublayers from my UIView and drawing the rotated layer, not working.
let layerZrotation = self.layer
self.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
if(doRotate == nil) { doRotate = 0}
layerZrotation.transform = CATransform3DMakeRotation(degree2radian(a:doRotate), 0.0, 0.0, 1.0)
self.layer.addSublayer(layerZrotation)
I expected a complete rotation when the phone is switched from portrait to landscape based on doRotate being set as follows:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
drawingViewTablet.doRotate = 90.0
buttonsShowing.doRotate = 90.0
} else {
drawingViewTablet.doRotate = -90.0
buttonsShowing.doRotate = -90.0
}
}
I calculate itemSize dependent on safe area for UICollectionView with horizontal scroll and custom layout.
But for iPhone X safe area has different size for different orientation. My question is how should I calculate safe area size for landscape orientation in viewWillTransition function? Or how is it possible to do without this calculation?
EDIT
To get safe area size without creating any additional views, use this:
view.safeAreaLayoutGuide.layoutFrame.size
If you want to use viewWillTransition method you can use this:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// Before rotation
print(view.safeAreaLayoutGuide.layoutFrame.size)
coordinator.animate(alongsideTransition: { (context) in
// During rotation
}) { (context) in
// After rotation
print(self.view.safeAreaLayoutGuide.layoutFrame.size)
}
}
In the completion block you will get your desired size, note however, that this code will be called after the rotation.
Original answer
Solution using additional UIView:
What I did was to create a UIView and pin it with constant 0 to Safe Area Guides, so that it always matches size of Safe Area:
I created an #IBOutlet of that UIView and in viewDidLayoutSubviews() check the size:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(containerView.frame.size)
}
After rotation I also get the updated size of that UIView.
Use UICollectionViewDelegateFlowLayout's collectionView(_:layout:sizeForItemAt:). Works like a charm.
Setting the size in the completion block of coordinator.animate(alongsideTransition:completion:) in viewWillTransition(to:with:) didn't work for me because the size animation occurs after the orientation animation ends, which looks weird.
I have a full-screen UIView. When I rotate the phone the view shrinks a little (and you see the black background) and then expands again to be full-screen. It basically animates as expected.
The other views on the screen also animate accordingly.
Can I prevent this from happening for one specific view?
I would like the full-screen view to just stay full-screen without animation revealing the black background, but maintain that the other views animate their rotation.
Kind of like how Apple does it in the camera app. The "viewfinder" does not animate its rotation, but the buttons do.
I have the following code in my ViewController. liveView is the the full-screen UIView mentioned.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animateAlongsideTransition(in: self.liveView, animation: { (context) in
if let connection = (self.liveView.layer as! AVCaptureVideoPreviewLayer).connection {
connection.videoOrientation = AVCaptureVideoOrientation(ui: UIApplication.shared.statusBarOrientation)
}
}, completion: nil)
}
AVCaptureVideoOrentation is an extension which basically translates UIInterfaceOrientation to AVCaptureVideoOrientation with a switch statement.
Thanks
- Joseph
Did you try self.liveView.layer.removeAllAnimations():
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animateAlongsideTransition(in: self.liveView, animation: { (context) in
if let connection = (self.liveView.layer as! AVCaptureVideoPreviewLayer).connection {
connection.videoOrientation = AVCaptureVideoOrientation(ui: UIApplication.shared.statusBarOrientation)
}
self.liveView.layer.removeAllAnimations()
}, completion: nil)
}
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)" )
}
}
I am looking for a way to enforce a UIPopoverPresentationController automatically resize to fill most of the screen, minus say 50 units of padding.
I first tried settings the preferredContentSize in viewDidLoad, and then detect when rotation occurs and update that CGSize to the new size I could calculate. But for some reason, this isn't working in my context. I was using UIDeviceOrientationDidChangeNotification which normally calls the method, but it's not called when the popover exists within a photo editing extension.
I am wondering if there's any other way to change the popover size besides manually updating the preferredContentSize after detecting a size change? If not, is there a way to detect rotation from within this popover in this context?
Try this:
func isLandscape() -> Bool {
// your logical here
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let myNewSize = isLandscape() ? mySizeLandscape : mySizePortrait
self.preferredContentSize = myNewSize
super.viewWillTransition(to: myNewSize, with: coordinator)
}