Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popoverpresentviewcontroller with rectangle corner and without arrow

I want a popover without rounded corners and with no arrow.

I have done the following code but it did not work:

//SerachPopViewController.swift

    //MARK: InitCoder
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    //popover settings
    //popoverPresentationController!.permittedArrowDirections = .Any
    modalPresentationStyle = .Popover
    popoverPresentationController!.delegate = self
    //permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
    self.preferredContentSize = CGSize(width:340,height:380)
}

//QueryTableViewController.swift

    @IBAction func searchFilter(sender: AnyObject) {

    let searchPopController = storyboard!.instantiateViewControllerWithIdentifier("SerachPopViewController") as! SerachPopViewController
    searchPopController.serachPopDelegate = self
    searchPopController.modalPresentationStyle = .Popover
    searchPopController.preferredContentSize = CGSize(width:340,height:380)

    let popoverPresentationController = searchPopController.popoverPresentationController

    popoverPresentationController!.sourceView = self.view;
    popoverPresentationController!.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
    popoverPresentationController!.permittedArrowDirections = UIPopoverArrowDirection();
    self.presentViewController(searchPopController, animated: true, completion: nil)
}

I am able to display popover view with arrow and rounded arrow.

Please help me to achieve:

  • popup view with rectangle corner
  • popup view without direction arrows
like image 326
Suraj Lokhande Avatar asked Jul 26 '16 14:07

Suraj Lokhande


4 Answers

Using the concept above, you can also set the corner radius in the completion parameter.

Swift 3

let popoverViewController: UIViewController = // Some view controller to be presented in a popover

// Set popover properties here...
// i.e. popoverViewController.modalPresentationStyle = .popover

present(popoverViewController, animated: true, completion: {
     popoverViewController.view.superview?.layer.cornerRadius = 0

    // Additional code here
})
like image 68
shawnynicole Avatar answered Nov 20 '22 19:11

shawnynicole


In iOS 11 its not possible to use @SHN solution for removing rounded corners. The corner radius is set to default value after viewWillAppear.

Radius must be set in viewDidAppear method

override func viewDidAppear(_ animated: Bool) {
    view.superview?.layer.cornerRadius = 0
    super.viewDidAppear(animated)
}
like image 35
crinos Avatar answered Nov 20 '22 19:11

crinos


To get popover without arrow when you are initiating popover, use:

popover!.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)

For popover without corner radius, in the popover content view controller use:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // Do any additional setup after loading the view, typically from a nib.
        self.view.superview?.layer.cornerRadius = 0.0;
    }
like image 4
SHN Avatar answered Nov 20 '22 19:11

SHN


I was not 100% happy with shawnynicole's answer because I realized the change from the rounded to the rectangular corners are notable/visible.

So I came up with this: subclass the view controller (in my case it was an UINavigationController) and override viewDidLayoutSubviews and update corners there. This is better because corners change animation is not visible and it will be updated every time needed (on rotations, etc).

It works in iOS11 and probably should work on others versions too.

class PopoverNavigationController: UINavigationController {

    @IBInspectable var cornerRadius: Int = -1

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let r = CGFloat(cornerRadius)
        if r >= 0 && view.superview?.layer.cornerRadius != r {
            view.superview?.layer.cornerRadius = r
        }
    }
}
like image 2
nacho4d Avatar answered Nov 20 '22 19:11

nacho4d