UITableView的滚动视图插图在iOS 13设备中无法调整

时间:2019-10-07 18:56:00

标签: ios swift ios13

我有一个显示5个标签的标签栏控制器。如图所示,它在iOS 12或更早版本中可以完美显示

here

但是,如果我在iOS 13上运行相同的软件,则设计完全混乱了

this

它完全失去了滚动视图插图,并且也没有调整菜单表视图的底部插图。一些菜单隐藏在底部。他们没有向上滚动。这是我在UITabBarController中使用的代码

override func viewDidLoad() {
    super.viewDidLoad()
    delegate = self



    guard let user = user else {
        appDelegate.instituteSelectionView()
        return
    }

    var dashBoard: UINavigationController
    var menus: UINavigationController? = nil

    // Creating all the view controllers for tabs.
    if user.role.caseInsensitiveCompare("student") == .orderedSame {
        let dashBoardVC = mainStoryBoard.instantiateViewController(withIdentifier: "DashboardVC") as! DashboardVC
        dashBoardVC.menus = ContentProvider.getMenus()
        dashBoardVC.school = user.school
        dashBoard = UINavigationController(rootViewController: dashBoardVC)
        dashBoard.navigationBar.prefersLargeTitles = true
    } else {
        let dashBoardVC = mainStoryBoard.instantiateViewController(withIdentifier: "DashboardWidgetsTableViewController") as! DashboardWidgetsTableViewController
        dashBoardVC.user = user
        dashBoardVC.title = "Dashboard"
        dashBoard = UINavigationController(rootViewController: dashBoardVC)

        let menusVC = mainStoryBoard.instantiateViewController(withIdentifier: "MenusTableViewController") as! MenusTableViewController
        menusVC.setMenus(ContentProvider.getMenusForStaff())
        menus = UINavigationController(rootViewController: menusVC)
    }
    dashBoard.tabBarItem = UITabBarItem(title: "Dashboard", image: #imageLiteral(resourceName: "dashboard"), tag: 0)

    if menus != nil {
        menus?.navigationBar.prefersLargeTitles = true
        menus?.tabBarItem = UITabBarItem(title: "Menus", image: #imageLiteral(resourceName: "menu-1"), tag: 1)
    }
    let notificationVC = mainStoryBoard.instantiateViewController(withIdentifier: "NotificationsTabTableViewController") as! NotificationsTabTableViewController
    notificationVC.user = user
    let notification = UINavigationController(rootViewController: notificationVC)
    notification.tabBarItem = UITabBarItem(title: "Notifications", image: #imageLiteral(resourceName: "bell"), tag: 3)
    notification.tabBarItem.badgeValue = user.badge > 0 ? String(user.badge) : nil
    notification.navigationBar.prefersLargeTitles = true

    let settingsVC = mainStoryBoard.instantiateViewController(withIdentifier: "SettingsTabTableViewController") as! SettingsTabTableViewController
    settingsVC.user = user
    let settings = UINavigationController(rootViewController: settingsVC)
    settings.tabBarItem = UITabBarItem(title: "Settings", image: #imageLiteral(resourceName: "settings"), tag: 4)
    settings.navigationBar.prefersLargeTitles = true

    let accountsVC = mainStoryBoard.instantiateViewController(withIdentifier: "AccountsTableViewController") as! AccountsTableViewController
    let accounts = UINavigationController(rootViewController: accountsVC)
    accounts.tabBarItem = UITabBarItem(title: "Accounts", image: #imageLiteral(resourceName: "user_group_man_man"), tag: 5)
    accounts.navigationBar.prefersLargeTitles = true


    mViewControllers = [dashBoard, notification, settings, accounts]
    if menus != nil {
        mViewControllers.insert(menus!, at: 1)
    }

    // Adding logo and profile button to navigation bar of each view controller.
    for vc in mViewControllers {
        if let nVC = vc as? UINavigationController, let vc = nVC.topViewController {
            let logo = UIImageView(image: #imageLiteral(resourceName: "educare logo"))
            logo.contentMode = .scaleAspectFill
            logo.clipsToBounds = true
            logo.widthAnchor.constraint(equalToConstant: 24).isActive = true
            logo.heightAnchor.constraint(equalToConstant: 24).isActive = true

            let profileButton = UIButton(type: .custom)
            profileButton.widthAnchor.constraint(equalToConstant: 24).isActive = true
            profileButton.heightAnchor.constraint(equalToConstant: 24).isActive = true
            profileButton.imageView?.layer.cornerRadius = 12
            if #available(iOS 13.0, *) {
                let image = UIImage(systemName: "person.crop.circle")
                profileButton.sd_setImage(with: URL(string: user.image), for: .normal, placeholderImage: image)
            } else {
                profileButton.sd_setImage(with: URL(string: user.image), for: .normal, placeholderImage: #imageLiteral(resourceName: "user_circle"))
            }

            profileButton.addTarget(self, action: #selector(openProfile), for: .touchUpInside)

            let logoButtonItem = UIBarButtonItem(customView: logo)
            let profileButtonItem = UIBarButtonItem(customView: profileButton)

            vc.navigationItem.leftBarButtonItem = logoButtonItem
            vc.navigationItem.rightBarButtonItems = [profileButtonItem]
        }
    }
    setViewControllers(mViewControllers, animated: true)
}

更新:实际上,标签栏控制器中的第一个视图控制器可以正确呈现,但其余视图控制器具有难看的导航栏。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。原来是导致问题的原因是过渡动画(从初始视图控制器到主选项卡栏控制器)。所以我从这里更改了代码

if animated {
        UIView.transition(from: (window?.rootViewController?.view)!, to: viewController.view, duration: duration, options: animationOptions) { (completed) in
            if completed {
                self.window?.rootViewController = viewController
                self.window?.makeKeyAndVisible()
            }
        }
    } else {
        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()
    }

对此

guard let window = UIApplication.shared.keyWindow else { return }
    window.rootViewController = viewController
    window.makeKeyAndVisible()
    if animated {
        UIView.transition(with: window, duration: duration, options: animationOptions, animations: {})
    }