21

我对 iOS 7 有一个问题,这似乎是一个错误,或者我只是没有做正确的事情。我有 modalViewController,它在 iPad 上显示为带有 ModalPresentationStyle 的弹出框。而且它不是标准尺寸,定制尺寸。这是代码:

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 它工作正常。但仅限于这种模式。也许有人也偶然发现了这个并且知道如何解决它?我不是溶解效果的忠实粉丝。谢谢你。

4

6 回答 6

39

我有同样的问题。我已经通过使用另一种方法解决了这个问题,在此处找到。

该解决方案提出的是使用该方法(void)viewWillLayoutSubviews

所以如果@Manuel M. 在里面GeneralSettingsViewController添加下面的代码:

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

而且您将不再需要此代码:

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

对于@titicaca,您使用的是UINavigationController我尚未使用此控制器对其进行测试,但您可以尝试我提到的相同解决方案,扩展UINavigationController并覆盖该viewWillLayoutSubviews方法。

[编辑]

对于@titicaca,我在一个新项目中尝试了它,对我来说它有效。我所做的是有一个自定义导航视图控制器CustomNavigationController覆盖viewWillLayoutSubviews这样的:

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

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

UIViewController *myVC = [[UIViewController alloc] init];
[myVC.view setBackgroundColor:[UIColor redColor]];

CustomNavigationController *nav = [[CustomNavigationController alloc] initWithRootViewController:myVC];
[nav setModalPresentationStyle:UIModalPresentationFormSheet];
[nav setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];

[self presentViewController:nav animated:YES completion:nil];

但是,您需要确保它们的尺寸self.view.superview.bounds = CGRectMake(0, 0, 330, 284);是偶数,否则里面的文本会变得模糊,如果有的话

于 2013-09-26T11:22:32.500 回答
5

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

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.userNameTextField becomeFirstResponder];
    });
}
于 2014-03-21T17:27:39.823 回答
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;

}

于 2013-09-22T11:13:38.400 回答
3

我有一种方法可以使用旧的自定义模态演示样式fromsheetiOS <=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();
    });
}
于 2013-10-02T09:51:01.317 回答
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];
      }
于 2014-03-22T06:47:32.977 回答
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)
    }

}
于 2014-12-25T14:55:23.247 回答