How to Center a Popoverview in Swift

how to center a popoverview in swift

You need to provide the source rect for the popover.

From the apple documentation: the source rect is the rectangle in the specified view in which to anchor the popover. Use this property in conjunction with the sourceView property to specify the anchor location for the popover.

In your case, under

_popoverPresentationController.sourceView = self.view;

add:

_popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)

It will do the trick!

Sample Image

How center popover on TableView?

I find the way to solve this, changing the popover.sourceRect, instead
of completing de x and y position with self.view.frame replaced for self.view.bounds

let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.delegate = self
popover.sourceView = self.view

popover.sourceRect = CGRect(x: (self.view.bounds.midX - (width/2)), y: (self.view.bounds.midY - (height/2)), width: width, height: height)

Here I leave you the difference between frame and bounds:

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

I got it from here

Presenting a popup view controller programatically using auto layout

If you are not adding constraints yourself - which, in this case, you are not - you still need to tell the view how to behave.

Change your createSubviews() function to this:

func createSubViews() {
//translatesAutoresizingMaskIntoConstraints = false

autoresizingMask = [.flexibleHeight, .flexibleWidth]

backgroundColor = .yellow

addSubview(blueBox)

blueBox.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
blueBox.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
blueBox.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.7).isActive = true
blueBox.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.4).isActive = true

}

That will allow auto-layout to re-layout your view(s) on device rotation.

How to change the size of a popover

Set the preferred content size on the view controller being presented not the popoverPresentationController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) // func for popover
{
if segue.identifier == "popoverView"
{
let vc = segue.destinationViewController

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

let controller = vc.popoverPresentationController

controller?.delegate = self
//you could set the following in your storyboard
controller?.sourceView = self.view
controller?.sourceRect = CGRect(x:CGRectGetMidX(self.view.bounds), y: CGRectGetMidY(self.view.bounds),width: 315,height: 230)
controller?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)

}
}

How to position UIPopoverArrowDirection to TOP RIGHT of the popover view

This is not built into UIKit, only the directions defined in the UIPopoverArrowDirection enum are supported.

If you define your own view (and popover controller) subclasses, you may be able to define such behavior by explicitly drawing a triangle where you want.

Popoverview is not centred on iPad rotation

popoverController must be also assigned in second alert:

if let popoverController = alert.popoverPresentationController {
self.popoverController = popoverController
...


Related Topics



Leave a reply



Submit