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.
Đâ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
Nguồn
2018-01-10 05:54:23
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