Search code examples
swiftpopover

change the width of the popover in swift


I want to change the width of the popover and keep the height is same

for example, if you open popover on iPad, the height is something like 70%-80% of the screen. I want to keep it same, but need to increase the width

I can use this block of clode, but here I have to enter the height as well, which is I don't want.

let vc = segue.destinationViewController
vc.preferredContentSize = CGSize(width: 200, height: 300)

Thank you so much.


Solution

  • Make height adjust to the size of the screen

    var heightVariable = (self.view.frame.size.height * 70) / 100
    

    Now, with heightVariable , height gonna always be %70 of the screen height.So you dont need to give constant to height

    let vc = segue.destinationViewController
    vc.preferredContentSize = CGSize(width: 200, height: heightVariable) // change width
    

    By the way ,You can detect device and change view's properties with using UIDevice.current.userInterfaceIdiom like :

    if UIDevice.current.userInterfaceIdiom == .pad{
        // change width
    
     }else if UIDevice.current.userInterfaceIdiom == .phone{
    // change width
     }