Popover with ModalPresentationStyle is not centered in iOS 7 iPad

前端 未结 6 1367
悲哀的现实
悲哀的现实 2021-01-31 19:18

I have a problem with iOS 7 that seems to be a bug or I just don\'t do something right. I have modalViewController that appears as a popover on iPad with ModalPresentationStyle.

6条回答
  •  离开以前
    2021-01-31 19:35

    I went about this slightly differently using a subclassed navigation controller and AutoLayout in Swift, to center the view in the superview at a set width and height. My particular case needed iPad and iOS7-8 support (there's some constants specific to my project in this code, but you get the idea) ...

    class CenteredModalNavigationController: UINavigationController {
    
        // MARK:- Methods
    
        override func viewDidLoad() {
            super.viewDidLoad()
            preferredContentSize = CGSizeMake(320, 480) // iOS8 only
        }
    
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
    
            // iOS7 support for iPad custom sized form-sheet modal.
            if kDeviceiOS7 && kDeviceiPad {
                if view.superview == nil {
                    Log.warning("Failed to find superview")
                    return
                }
                view.superview!.setTranslatesAutoresizingMaskIntoConstraints(false)
    
                // Give the superview constraints to center the view inside it.
                var viewBindingsDict: NSMutableDictionary = NSMutableDictionary()
                viewBindingsDict.setValue(view, forKey: "view")
                viewBindingsDict.setValue(view.superview!, forKey: "superview")
                let centerXConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[superview]-(<=1)-[view]",
                    options: .AlignAllCenterX,
                    metrics: nil,
                    views: viewBindingsDict)
                view.superview!.addConstraints(centerXConstraints)
    
                let centerYConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:[superview]-(<=1)-[view]",
                    options: .AlignAllCenterY,
                    metrics: nil,
                    views: viewBindingsDict)
                view.superview!.addConstraints(centerYConstraints)
    
                // Now give it a width/height and it should float in the middle of our superview.
                AutoLayoutHelper.addWidthConstraint(view,
                    aSuperView: view.superview!,
                    width: 320)
                AutoLayoutHelper.addHeightConstraint(view,
                    aSuperView: view.superview!,
                    height: 480)
            }
        }
    
        override func prefersStatusBarHidden() -> Bool {
            return true
        }
    
        override func disablesAutomaticKeyboardDismissal() -> Bool {
            // DEVNOTE: Because this is shown in a modal, this is required to stop keyboard dismiss issues.
            return true
        }
    
    }
    

    ...

    class AutoLayoutHelper: NSObject {
    
        class func addHeightConstraint(aChildView:UIView, aSuperView:UIView, height:CGFloat) {
            var constraint = NSLayoutConstraint(item: aChildView,
                attribute: NSLayoutAttribute.Height,
                relatedBy: NSLayoutRelation.Equal,
                toItem: nil,
                attribute: NSLayoutAttribute.NotAnAttribute,
                multiplier: 1.0,
                constant: height)
            aSuperView.addConstraint(constraint)
        }
    
        class func addWidthConstraint(aChildView:UIView, aSuperView:UIView, width:CGFloat) {
            var constraint = NSLayoutConstraint(item: aChildView,
                attribute: NSLayoutAttribute.Width,
                relatedBy: NSLayoutRelation.Equal,
                toItem: nil,
                attribute: NSLayoutAttribute.NotAnAttribute,
                multiplier: 1.0,
                constant: width)
            aSuperView.addConstraint(constraint)
        }
    
    }
    

提交回复
热议问题