【问题标题】:iOS 11 Popover Presentation Issue in Multi-tasking ModeiOS 11 多任务模式下的弹出框演示问题
【发布时间】:2018-05-12 09:25:58
【问题描述】:

保持弹出框处于打开状态。然后,当我尝试切换到 2/3 屏幕模式并在 viewWillTrainsition 中使用固定空间 BarButtonItem 更改中心 BarButtonItem 的位置时,我的弹出框工具提示将移动到 barButtonItem 的上一个位置。

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
    if (size.width>size.height) {
        _fixedSpace.width = 280;
    } else {
        _fixedSpace.width = 80;
    }
} completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {

}];}

【问题讨论】:

  • 您是否尝试在移至其他屏幕之前关闭当前弹出框?

标签: ios objective-c ios11 uipopovercontroller xcode9


【解决方案1】:

从 iOS11 开始,条形按钮项使用约束而不是框架。因此,请尝试对每个条形按钮项进行约束。它可能不会从视觉上反映出来,但它在产生此类问题时起着重要作用。

尝试使用以下代码设置约束:

if #available(iOS 11.0, *)
{
  _fixedSpace.widthAnchor.constraint(equalToConstant: 280.0).isActive = true
}

希望这会有所帮助!

【讨论】:

【解决方案2】:

我复制了相同的场景,并找到了解决方法,如下所示:

[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
    if (size.width > size.height) {
        fixedSpace.width = 280;
    } else {
        fixedSpace.width = 80;
    }
} completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
    CGRect rect = [self.view convertRect:barBtn.frame fromView:barBtn];
    popover.sourceRect = rect;
}];

尝试在完成块中重置 sourceRect 属性。

希望对您有所帮助!

【讨论】:

    【解决方案3】:

    据我了解您的问题,我想为此提出一个简单的解决方案:

    我在情节提要中使用了 Viewcontroller 和 PopoverView。 Viewcontroller 将作为主视图控制器工作,而 PopoverView 将作为弹出窗口显示。 (注意:不要忘记在 StoryBoard 中为 PopoverView 设置显式内容大小)以获取更多参考,您可以查看我的故事板的附加屏幕截图。

    这里是 Viewcontroller 的示例源代码,您会发现 Popoverview 的位置会随着右键框架的变化而变化。

    此代码是使用 Objective C

    开发的

    ViewController.h

        //
        //  ViewController.h
        //  SOPopoverControllerDemo
        //
        //  Created by Test User on 08/01/18.
        //  Copyright © 2018 Test User All rights reserved.
        //
    
        #import <UIKit/UIKit.h>
    
        @interface ViewController : UIViewController
    
    
        @end
    

    ViewController.m

        //
        //  ViewController.m
        //  SOPopoverControllerDemo
        //
        //  Created by Test User on 08/01/18.
        //  Copyright © 2018 Test User All rights reserved.
        //
    
        #import "ViewController.h"
    
        @interface ViewController () <UIPopoverPresentationControllerDelegate>
    
        @property (weak, nonatomic) IBOutlet UIBarButtonItem *leftToolBarBtn;
        @property (weak, nonatomic) IBOutlet UIBarButtonItem *rightToolBarBtn;
        @property (weak, nonatomic) IBOutlet UIBarButtonItem *flexibleBtn;
        @property (weak, nonatomic) IBOutlet UIToolbar *bottomToolBar;
        @property (weak,nonatomic) UIPopoverPresentationController *popOverController;
    
    
        @end
    
    
    
        @implementation ViewController
    
        - (void)viewDidLoad {
            [super viewDidLoad];
            // Do any additional setup after loading the view, typically from a nib.
    
            if (self.view.frame.size.width > self.view.frame.size.height) {
                self.rightToolBarBtn.width = 200;
                self.leftToolBarBtn.width = 200;
            } else {
                self.rightToolBarBtn.width = 150;
                self.leftToolBarBtn.width = 150;
            }
    
        }
    
        - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
            [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
            [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
                if (size.width > size.height) {
                    _rightToolBarBtn.width = 200;
                    [self dismissViewControllerAnimated:true completion:nil];
                    [self rightToolBarBtnTapped:_rightToolBarBtn];
                } else {
                    _rightToolBarBtn.width = 150;
                    [self dismissViewControllerAnimated:true completion:nil];
                    [self rightToolBarBtnTapped:_rightToolBarBtn];
                }
            } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
    
            }];
        }
    
        - (IBAction)rightToolBarBtnTapped:(id)sender {
    
    
            //Grab the controller for popover
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"PopoverView"];
    
            if (self.view.frame.size.width > self.view.frame.size.height ) {
                controller.preferredContentSize = CGSizeMake(200, 100);
            } else {
                controller.preferredContentSize = CGSizeMake(150, 100);
            }
    
    
            controller.modalPresentationStyle = UIModalPresentationPopover;
            [self presentViewController:controller animated:YES completion:nil];
    
            // configure the Popover presentation controller
            _popOverController = [controller popoverPresentationController];
            _popOverController.delegate = self;
            _popOverController.permittedArrowDirections = UIPopoverArrowDirectionUp;
            _popOverController.barButtonItem = self.rightToolBarBtn;
    
            }
    
    
        //--------------------------------------------------
        #pragma mark ->  UIPopOverController Delegate
        //--------------------------------------------------
    
        - (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
            return YES;
        }
    
        - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
            NSLog(@"Popover Did Dismissed");
        }
    
    
        @end
    

    PopoverView.h

        //
        //  PopoverView.h
        //  SOPopoverControllerDemo
        //
        //  Created by Test User on 08/01/18.
        //  Copyright © 2018 Test User All rights reserved.
        //
    
        #import <UIKit/UIKit.h>
    
        @interface PopoverView : UIViewController
    
        @end
    

    PopoverView.m

        //
        //  PopoverView.m
        //  SOPopoverControllerDemo
        //
        //  Created by Test User on 08/01/18.
        //  Copyright © 2018 Test User All rights reserved.
        //
    
        #import "PopoverView.h"
    
        @interface PopoverView ()
    
        @end
    
        @implementation PopoverView
    
        - (void)viewDidLoad {
            [super viewDidLoad];
            // Do any additional setup after loading the view.
        }
    
        - (void)didReceiveMemoryWarning {
            [super didReceiveMemoryWarning];
            // Dispose of any resources that can be recreated.
        }
    
        @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      相关资源
      最近更新 更多