2

Whenever I push to a new View Controller, a weird animation glitch where the title stays even after another view controller is loaded and does not animate to the back button. It only happens while pushing to a View Controller when large title is showing. I cannot understand why it's happening.

This is the Glitch I get.

This is my Main View Controller.

'''

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var tblUserList: UITableView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var constTableViewHeight: NSLayoutConstraint!

let searchController = UISearchController(searchResultsController: nil)

let names = ["Rajesh Hamal", "Rishi Dhamala", "Salman Khan", "Shilpa Shetty", "Majnu Bhai", "Uday Shetty","Rajesh Hamal", "Rishi Dhamala", "Salman Khan", "Shilpa Shetty", "Majnu Bhai", "Uday Shetty"]

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.searchController = searchController
    tblUserList.register(UINib(nibName: "UserListCell", bundle: nil), forCellReuseIdentifier: "UserListCell")
    }
override func viewWillAppear(_ animated: Bool) {
    navigationController?.navigationBar.prefersLargeTitles = true
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.constTableViewHeight?.constant = self.tblUserList.contentSize.height
}

@IBAction func btnAddUser(_ sender: UIButton) {
    let vc = UIStoryboard(name: "AddUser", bundle: nil).instantiateViewController(withIdentifier: "AddUser") as! AddUser
    navigationController?.pushViewController(vc, animated: true)
}

} '''

And this is the view controller that I am pushing to:

''' import UIKit import IBAnimatable

class AddUser: UIViewController, UIImagePickerControllerDelegate & UINavigationControllerDelegate {

@IBOutlet weak var imgProfilePicture: AnimatableImageView!
override func viewDidLoad() {
    super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
    navigationController?.navigationBar.prefersLargeTitles = false
}

@IBAction func btnChooseProfilePicture(_ sender: UIButton) {
    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
            alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
                self.openCamera()
            }))
            alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
                self.openGallery()
            }))
            alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

            self.present(alert, animated: true, completion: nil)
}

func openGallery()
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.allowsEditing = true
        imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have permission to access gallery.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let pickedImage = info[.originalImage] as? UIImage {
        self.imgProfilePicture.image = pickedImage
    }
    picker.dismiss(animated: true, completion: nil)
}
func openCamera()
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.camera
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

}

'''

4
  • In ViewController try to implement viewWillDisappear. There you should be able to set navigationController?.navigationBar.prefersLargeTitles = false. Commented Sep 16, 2021 at 11:17
  • @finebel I have already tried it and didn't work. Commented Sep 16, 2021 at 14:51
  • Did you try to set it before navigationController?.pushViewController(vc, animated: true)? Commented Sep 16, 2021 at 15:33
  • This should help you: stackoverflow.com/questions/59217253/… Commented Sep 17, 2021 at 7:31

1 Answer 1

1

For me navigationBar.layoutIfNeeded() fixed the issue (in addition to prefersLargeTitles = false):

  override func viewWillDisappear(_ animated: Bool) {
    navigationController?.navigationBar.prefersLargeTitles = false
    navigationController?.navigationBar.sizeToFit()
    (navigationController as? ESNavigationController)?.removeLargeTitleView()
    navigationController?.navigationBar.layoutIfNeeded()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.