Sunday, November 8, 2026
HomeiOS Developmentios - present a view controller like a iPhone's popover in iPad

ios – present a view controller like a iPhone’s popover in iPad

[ad_1]

I need to current a brand new view controller in a popover view utilizing an iPad.

I created a CustomTabbaroCntroller class and hooked up a newEventButton to it. Once I add a button motion in addEventButtonAction operate, I set the modalPresentationStyle as a popover, and it really works completely if I run on iPhone. Nevertheless, once I run on the iPad and once I faucet the newEventButton, the app is terminated saying

Terminating app because of uncaught exception ‘NSGenericException’, purpose: ‘UIPopoverPresentationController (<UIPopoverPresentationController: 0x105767bc0>) ought to have a non-nil sourceView or barButtonItem set earlier than the presentation happens.’
terminating with uncaught exception of kind NSException

I’ve carried out someresearch and I figured I would like to find out the place to position the popover, so I add the next line within the code however the app nonetheless crashes once I faucet the newEventButton and offers me the identical error I’ve proven.

So my query is the way to show a brand new view controller utilizing popover on iPad, just like the popover in iPhone (I imply the one covers like 90% of the display screen)?

class CustomTabBarController: UITabBarController {
    
    var createEventViewController = CreateEventViewController()

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        setupCustomTabBar()
        setupNewEventButton()
    }
    
    func setupCustomTabBar() {
        self.tabBar.shadowImage = UIImage()
        self.tabBar.backgroundImage = UIImage()
        let tabBarView = UIImageView(picture: Pictures.tabbarImage)
        self.tabBar.addSubview(tabBarView)
        tabBarView.fillSuperview()

        // add VCs to customized tabbar
        let ListVC = ListViewController()
        let nav1 = UINavigationController(rootViewController: ListVC)
        viewControllers = [nav1]
    }
    
    func setupNewEventButton() {
        let newEventButton = UIButton()
        let newEventBtnImage = Pictures.newEvent
        
        view.addSubview(newEventButton)
        
        newEventButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            // constraints
        ])
        
        newEventButton.setImage(newEventBtnImage, for: .regular)
        newEventButton.addTarget(self, motion: #selector(addEventButtonAction(sender:)), for: .touchUpInside)
        
        view.layoutIfNeeded()
    }
    
    @objc personal func addEventButtonAction(sender: UIButton) {
        let vc = CreateEventViewController()
        let nc = UINavigationController(rootViewController: vc)
        nc.modalPresentationStyle = .popover
        
        let screenWidth = UIScreen.major.bounds.width
        let screenHeight = UIScreen.major.bounds.peak

        nc.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: screenWidth, peak: screenHeight)
        self.current(nc, animated: true, completion: nil)
    }

}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments