2017-11-28 23 views
5

Giữ cửa sổ bật lên ở trạng thái mở. Sau đó, khi tôi cố gắng chuyển sang chế độ màn hình 2/3 và thay đổi vị trí của trung tâm BarButtonItem với không gian cố định BarButtonItem trong viewWillTrainsition, công cụ popover của tôi di chuyển đến vị trí trước đó của barButtonItem.Sự cố trình bày Popover của iOS 11 ở Chế độ đa tác vụ

- (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) { 

}];} 

enter image description here

+0

Bạn đã thử bằng cách gạt bỏ popover hiện trước khi chuyển sang màn hình khác? – Bucket

Trả lời

-1

Từ iOS11, thanh nút mục được sử dụng hạn chế thay vì khung. Vì vậy, hãy thử đưa ra một ràng buộc cho mỗi mục nút thanh. Nó có thể không phản ánh trực quan nhưng nó đóng một vai trò quan trọng trong việc sản xuất loại vấn đề này.

Hãy thử sử dụng mã dưới đây để thiết lập các hạn chế:

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

Hope this helps!

+0

Không có anchor width cho barButton. – Rajneesh071

+0

Lời xin lỗi của tôi, tôi đã xem tùy chỉnh hỗ trợ widthAnchor vì vậy nghĩ rằng nó sẽ làm việc. Dù sao, hãy thử https://stackoverflow.com/questions/45544961/negative-spacer-for-uibarbuttonitem-in-navigation-bar-on-ios-11 này – torap

0

Tôi lặp lại cùng một kịch bản và tôi thấy một sửa chữa cho điều này như sau:

[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; 
}]; 

Cố gắng thiết lập lại tài sản sourceRect trong khối hoàn thành.

Hy vọng điều này hữu ích!

0

Theo như tôi hiểu câu hỏi của bạn tôi muốn đề xuất một giải pháp đơn giản cho việc này:

Tôi đã mất một ViewController và PopoverView trong kịch bản này. Viewcontroller sẽ hoạt động như bộ điều khiển xem chính, nơi PopoverView sẽ hoạt động để hiển thị dưới dạng popover. (Thận trọng: Đừng quên đặt Kích thước nội dung rõ ràng cho PopoverView trong StoryBoard) để tham khảo thêm, bạn có thể xem ảnh chụp màn hình kèm theo của bảng phân cảnh của tôi.

enter image description here

Đây là mã nguồn mẫu ViewController trong đó bạn sẽ tìm thấy vị trí Popoverview sẽ thay đổi theo sự thay đổi khung nút bên phải.

Mã này được phát triển sử dụng 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 
Các vấn đề liên quan