ModalPresentationStyle的Popover不在iOS 7 iPad中居中

时间:2013-09-22 04:45:59

标签: objective-c ipad ios7 modalviewcontroller uimodalpresentationstyle

我有iOS 7的问题似乎是一个错误,或者我只是不做正确的事情。我有modalViewController,它在iPad上使用ModalPresentationStyle显示为popover。它不是标准尺寸,自定义尺寸。 这是代码:

myViewController *myVC = [[myViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myVC];
[nav setModalPresentationStyle:UIModalPresentationFormSheet];
[nav setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
[self presentViewController:nav animated:YES completion:nil];
nav.view.superview.bounds = CGRectMake(0, 0, 320, 465);

这在iOS 6中都运行良好,但在iOS 7中并没有集中。 但是,如果我将ModalTransitionStyle设置为UIModalTransitionStyleCrossDissolve,它可以正常工作。但只有在这种模式下。 也许有人偶然发现了这个,知道如何修复它?我不是解散效果的忠实粉丝。 谢谢。

6 个答案:

答案 0 :(得分:39)

我遇到了同样的问题。我已经通过另一种方法解决了这个问题,找到了here

此解决方案建议使用方法(void)viewWillLayoutSubviews

如果@ {1}}内的@Manuel M.添加以下代码:

GeneralSettingsViewController

你不再需要这个代码了:

// GeneralSettingsViewController
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 497, 375);
}

对于@titicaca,您使用self.generalSettingsVC.view.superview.frame = CGRectMake(0, 0, 497, 375); self.generalSettingsVC.view.superview.center = self.view.center; 我还没有使用此控制器测试它,但您可以尝试我提到的相同解决方案,扩展UINavigationController并覆盖{{1方法。

<强> [编辑]

对于@titicaca我在一个新项目中尝试过,对我而言,它有效。我所做的是让自定义导航视图控制器UINavigationController覆盖viewWillLayoutSubviews,如下所示:

CustomNavigationController

然后,呈现viewWillLayoutSubviews的视图控制器应该执行类似于此的代码:

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 330, 284);
}

你需要确保CustomNavigationController的尺寸是偶数,否则里面的文字会变得模糊,如果有的话

答案 1 :(得分:5)

对我来说,问题是在呈现的视图控制器的becomeFirstResponder中的文本字段上调用viewDidAppear。现在似乎是一个错误。解决方案是将其包装在一个简单的dispatch_async

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.userNameTextField becomeFirstResponder];
    });
}

答案 2 :(得分:4)

对我来说也一样......我还不知道如何解决它。我目前正在处理这个问题所以我得到的任何东西我都会分享它!

这是我的代码。

-(IBAction)showGeneralSettings:(id)sender{

self.generalSettingsVC = [[GeneralSettingsViewController alloc] initWithNibName:@"GeneralSettingsView" bundle:nil];

//Present the view controller as a modal with a custom size (important to do after presenting it)
self.generalSettingsVC.modalPresentationStyle = UIModalPresentationFormSheet;

[self presentViewController:self.generalSettingsVC animated:YES completion:nil];

self.generalSettingsVC.view.superview.frame = CGRectMake(0, 0, 497, 375);
self.generalSettingsVC.view.superview.center = self.view.center;

}

答案 3 :(得分:3)

上述解决方案对我不起作用。我使用了以下内容:

      UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];
      navigationController.modalPresentationStyle=UIModalPresentationFormSheet;
      [self presentViewController:navigationController animated:YES completion:nil];
      if([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone){
            navigationController.view.superview.frame = CGRectMake(0, 0, 320, 544);
            navigationController.view.frame =CGRectMake(108, 0, 320, 544);
            navigationController.view.superview.backgroundColor=[UIColor clearColor];
      }

答案 4 :(得分:2)

我有一种方法,旧的自定义模式演示文稿样式fromsheet可以与iOS <=7一起使用,但您可以设置custom height and width

请注意,此方法未来可能无法在任何较新版本中使用

- (void) hackModalSheetSize:(CGSize) aSize ofVC:(UIViewController *) aController;
{

    void (^formSheetBlock) (void) = ^{
        int preferredWidth = aSize.width;
        int preferredHeight = aSize.height;

        CGRect frame = CGRectMake((int) 1024/2 - preferredWidth/2,
                                  (int) 768/2 - preferredHeight/2,
                                  preferredWidth, preferredHeight);
        aController.view.superview.frame = frame;
        if([aController respondsToSelector:@selector(edgesForExtendedLayout)]) { //ios7
            aController.view.superview.backgroundColor = [UIColor clearColor];
        } else { // < ios7
            UIImageView *backgroundView = [aController.view.superview.subviews objectAtIndex:0];
            [backgroundView removeFromSuperview];
        }
    };

    //on ios < 7 the animation would be not as smooth as on the older versions so do it immediately
    if(![self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        formSheetBlock();
        return;
    }

    double delayInSeconds = .05;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        formSheetBlock();
    });
}

答案 5 :(得分:1)

我在Swift中使用子类导航控制器和AutoLayout略微改变了这一点,使视图在超视图中居中设置的宽度和高度。我的特殊情况需要iPad和iOS7-8支持(在此代码中有一些特定于我的项目的常量,但你明白了这一点)......

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)
    }

}