Custom segue to a navigation controller - swift

I am using a custom segue to slide my views over. The custom subclass works fine whenever it moves from a view controller to a view controller, but it crashes when I try to move from a view controller to a navigation controller. (It carries over the previous navigation bar and when I try to click the screen it crashes.)
This is the custom segue
class FromRightSegue: UIStoryboardSegue {
override func perform() {
let src = self.source
let dst = self.destination
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: .curveEaseInOut,
animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
},
completion: { finished in
src.present(dst, animated: false, completion: nil)
}
)
}
}
When I use the built in segues like show the segue works fine, but there is no built in segue that moves from the right. Any suggestions?

Related

Segue From Right To Left Without Push Or Presenting ViewController

Is there any possibilities to perform segue from Right To Left Without push or Persenting ViewController. The Following Code Working Fine with animation but if I use this class my TabBar is hidden. If I remove the code inside the Perform() TabBar is show but the animation is stop
Using MDCBottomNavigationBar
class SegueFromRight: UIStoryboardSegue {
override func perform() {
let src = self.source
let dst = self.destination
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.25,delay: 0.0,options: UIView.AnimationOptions.curveEaseInOut,animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
},completion: { finished in
src.navigationController?.pushViewController(dst, animated: false)
}
)
}
}
is there any other way to perform segue with animation without hiding TabBar ?
Finally I found the answer. Just Removing the line Push ViewController
class SegueFromRight: UIStoryboardSegue {
override func perform() {
let src = self.source
let dst = self.destination
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.25,delay: 0.0,options: UIView.AnimationOptions.curveEaseInOut,animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
},completion: { finished in
//Remove Following line if you want to segue modally
//src.navigationController?.pushViewController(dst, animated: false)
}
)
}
}
Try this
private func setupAlwaysVisibleView() {
guard let tabBarController = self.tabBarController else { return }
tabBarController.view.insertSubview(alwaysVisibleView, belowSubview: tabBarController.tabBar)
}

Cant go back to original ViewController with .left swipe?

I have a viewcontroller that is embedded in a navigation controller. I wanted a Swipe right to go right left and change viewcontroller from VC1 to VC2, which thanks to people on stackoverflow i managed to do. However now i want to go the traditional way left right (traditional way for push), when swiping left on VC2 that goes back to VC1.
This is my code in VC1 (to swipe right to go to VC2):
override func viewDidLoad() {
super.viewDidLoad()
//Swipe right gesture
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(SelectedTopicViewController.performChange))
gesture.direction = .right
self.view.addGestureRecognizer(gesture)
}
#objc func performChange() {
self.performSegue(withIdentifier: "goTo", sender: nil)
}
This is my code in VC2 (for swiping left to go back to VC1):
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: false)
//swipe left gesture
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(SendMessageTopicViewController.performChange))
gesture.direction = .left
self.view.addGestureRecognizer(gesture)
// Do any additional setup after loading the view.
}
#objc func performChange() {
//self.navigationController?.popViewController(animated: true)
//print("swipe detected")
self.performSegue(withIdentifier: "goBackToRight", sender: nil)
}
Also this is my custom segue code (for both left and right swipe):
import Foundation
import UIKit
import QuartzCore
class SegueFromLeft: UIStoryboardSegue {
override func perform() {
let src: UIViewController = self.source
let dst: UIViewController = self.destination
let transition: CATransition = CATransition()
let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.duration = 0.25
transition.timingFunction = timeFunc
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
src.navigationController!.view.layer.add(transition, forKey: kCATransition)
src.navigationController!.pushViewController(dst, animated: false)
}
}
class SegueFromRight: UIStoryboardSegue {
override func perform() {
let src = self.source
let dst = self.destination
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width*2, y: 0) //Double the X-Axis
UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
}) { (finished) in
src.present(dst, animated: false, completion: nil)
}
}
}
And this is my storyboard, note that i have one segue going to VC2, and one going back to VC1 (both custom segue using the different classes).
Even if i have a print("swiped") when user swipes on VC2 it dosent get printed in console, hence why i think its something wrong with the swipes...
Conclusion: Swipe does work on VC1 and segue to VC2 works, however swipe on VC2 does not work at all...

Strange animation 'lag' after launch screen disappears and before viewDidAppear() method runs

After launch screen dismisses itself logo and title of my app (they are in container) should go closer to the top of the screen. Between dismissing launch screen and viewDidAppear method there is a strange 'blink' of my container in the background. As you can see I am using snapkit but it should have nothing to do with the problem. Here is my code:
class WelcomeScreenViewController: UIViewController {
var welcomeScreenView: WelcomeScreenView {
return view as! WelcomeScreenView
}
override func loadView() {
let contentView = WelcomeScreenView(frame: .zero)
view = contentView
}
override func viewDidLoad() {
super.viewDidLoad()
self.welcomeScreenView.checkWeatherButton.transform = CGAffineTransform(translationX: 0, y: 200)
self.welcomeScreenView.checkWeatherButton.addTarget(self, action: #selector(showCityChoiceVC), for: .touchUpInside)
navigationController?.isNavigationBarHidden = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.welcomeScreenView.appNameLogoContainerVerticalConstraint?.isActive = false
self.welcomeScreenView.appNameLogoContainer.snp.makeConstraints({ (make) in
make.top.equalTo(self.welcomeScreenView).offset(100)
})
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: {
self.welcomeScreenView.layoutIfNeeded()
self.welcomeScreenView.checkWeatherButton.transform = CGAffineTransform(translationX: 0, y: 0)
}, completion: nil)
}
#objc private func showCityChoiceVC() {
self.navigationController?.pushViewController(RegisterViewController(), animated: true)
}
Blinking comes from setting constraints in viewDidAppear. Use viewWillAppear or viewDidLoad instead. viewDidAppear is invoked when your view actually appears on screen. So any changes that happen will be visible to the user.

Custom Slide Segue - Xcode 8.0 Swift 3.0

What is the best way to create a custom segue that lets me press a button and then a view controller slides on. I have created a left to right segue but I want to make it go the other way. I have looked at some Youtube videos and this question but they don't show me what I want.
My code:
import UIKit
class SegueFromLeft: UIStoryboardSegue
{
override func perform()
{
let src = self.sourceViewController
let dst = self.destinationViewController
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransformMakeTranslation(-src.view.frame.size.width, 0)
UIView.animateWithDuration(0.25,
delay: 0.0,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: {
dst.view.transform = CGAffineTransformMakeTranslation(0, 0)
},
completion: { finished in
src.presentViewController(dst, animated: false, completion: nil)
}
)
}
}
Basically, I want to create a segue that goes from right to left.
Thanks
I am using Xcode 8.0 and Swift 3.0
Try this:
class SegueFromLeft: UIStoryboardSegue{
override func perform(){
let src=sourceViewController
let dst=destinationViewController
let slide_view=destinationViewController.view
src.view.addSubview(slide_view)
slide_view.transform=CGAffineTransform.init(translationX: src.view.frame.size.width, 0)
UIView.animateWithDuration(0.25,
delay: 0.0,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: {
slide_view.transform=CGAffineTransform.identity
}, completion: {finished in
src.present(dst, animated: false, completion: nil)
slide_view.removeFromSuperview()
})
}
}
Swift 3.1:
class ProceedToAppStoryboardSegue: UIStoryboardSegue {
override func perform(){
let slideView = destination.view
source.view.addSubview(slideView!)
slideView?.transform = CGAffineTransform(translationX: source.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: UIViewAnimationOptions.curveEaseInOut,
animations: {
slideView?.transform = CGAffineTransform.identity
}, completion: { finished in
self.source.present(self.destination, animated: false, completion: nil)
slideView?.removeFromSuperview()
})
}
}

Switching between UITextViews resets view Layout

I implemented some functions to move the UIView, whenever a TextField or a TextView is tapped. The height for the UIView to move upwards is calculated depending how much the keyboard would overlap the active TextField or TextView. When I tap outside of the TextField, or -View the keyboard will be dismissed and the View will be resetted. Now everything is working fine, but when I switch from one TextField directly to another above (without dismissing the keyboard) it seems like the UIView will return to the initial position, instead of just keeping the shifted view (because the now active TextView would not be overlapped by the keyboard since it is above the former). It looks like some method is called to reset the view, resulting in the keyboard overlapping the upper TextView. Is there a way to suppress this behavior?
private func observeKeyboardNotification(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardHide), name: .UIKeyboardWillHide, object: nil)
}
var distanceOfKeyboardToTextView: CGFloat = 0
var activeTextElement: UIView?
var viewIsShifted = false
func keyboardShow(notification: NSNotification){
findActiveTextField(subviews: self.view.subviews, textField : &activeTextElement)
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue, let activeTextElement = activeTextElement, !viewIsShifted {
let viewYPosition = (activeTextElement.superview?.convert(activeTextElement.frame.origin, to: nil).y)! + activeTextElement.frame.height
let keyboardYPosition = view.frame.height - keyboardSize.height
distanceOfKeyboardToTextView = viewYPosition - keyboardYPosition
if distanceOfKeyboardToTextView > 0 {
UIView.animate(withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1, options: .curveEaseOut,
animations: {
self.customView.frame = CGRect(x: 0, y: self.customView.frame.origin.y - self.distanceOfKeyboardToTextView, width: self.customView.frame.width, height: self.customView.frame.height)
}, completion: nil)
viewIsShifted = true
}
}
activeTextElement = nil
}
func keyboardHide(){
UIView.animate(withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1, options: .curveEaseOut,
animations: {
self.customView.frame = CGRect(x: 0, y: self.customView.frame.origin.y + self.distanceOfKeyboardToTextView, width: self.customView.frame.width, height: self.customView.frame.height)
}, completion: nil)
viewIsShifted = false
distanceOfKeyboardToTextView = 0
}
func findActiveTextField (subviews : [UIView], textField : inout UIView?) {
guard textField == nil else { return }
for view in subviews {
if view.isFirstResponder {
textField = view
break
}
else if !view.subviews.isEmpty {
findActiveTextField (subviews: view.subviews, textField: &textField)
}
}
}
Update:
After tapping from an active textfield directly onto another textfield, keyboardShow is call, but since the view is already shifed, UIView.animate will not be performed. However, the view is resetted like no keyboard would be displayed, but since the other textfield is active, the keyboard is visible.
I dismiss the keyboard using this extension:
extension UIViewController {
//functions to hide the keyboard
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}
and in my ViewController:
override func viewDidLoad() {
super.viewDidLoad()
observeKeyboardNotification()
self.hideKeyboardWhenTappedAround()
...
}