我正在尝试将ViewController显示为iPhone上的弹出窗口。我已经在SO和其他网络上找到了几个答案,但到目前为止还没有。我写了一个简单的应用程序来测试它。
ViewController.swift:
OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream());
out.write("string=" + stringToReverse);
out.close();
BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream()));
String decodedString; while ((decodedString = in.readLine()) != null) { System.out.println(decodedString); } in.close();
故事板中有一个嵌入在NavigationController中的空ViewController。
运行这个,我希望弹出视图控制器显示在“完成”按钮下。相反,蓝色视图控制器全屏显示。
有没有办法改变这种行为?
答案 0 :(得分:8)
您在呈现视图后连接代理。它将如何从委托返回。并显示为popover。使用此: -
func clicked(_ sender: Any) {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.blue
vc.modalPresentationStyle = .popover
vc.preferredContentSize = CGSize(width: 200, height: 200)
let ppc = vc.popoverPresentationController
ppc?.permittedArrowDirections = .any
ppc?.delegate = self
ppc?.barButtonItem = navigationItem.rightBarButtonItem
ppc?.sourceView = sender
present(vc, animated: true, completion: nil)
}
答案 1 :(得分:3)
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(clicked(_:)))
}
func clicked(_ sender: Any) {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.blue
vc.preferredContentSize = CGSize(width: 200, height: 200)
vc.modalPresentationStyle = .popover
let ppc = vc.popoverPresentationController
ppc?.permittedArrowDirections = .any
ppc?.delegate = self
ppc!.sourceView = sender as? UIView
ppc?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
答案 2 :(得分:0)
以上解决方案在最新的iOS 12及更高版本中不再起作用。为了使其再次正常工作,请在viewController中重写-modalPresentationStyle
以将其显示为弹出窗口,然后返回UIModalPresentationPopover
。
另外提供一个popoverPresentationController.delegate
,实现adaptivePresentationStyleForPresentationController:traitCollection:
并返回UIModalPresentationNone
。
@interface PopoverViewController : UIViewController
@end
@implementation PopoverViewController
- (UIModalPresentationStyle)modalPresentationStyle
{
return UIModalPresentationPopover;
}
@end
@interface ViewController ()<UIPopoverPresentationControllerDelegate>
@end
@implementation ViewController
- (IBAction)openPopover:(id)sender
{
UIViewController* testVC = [[PopoverViewController alloc] init];
testVC.view.backgroundColor = UIColor.yellowColor;
UIPopoverPresentationController* popPresenter = [testVC popoverPresentationController];
popPresenter.permittedArrowDirections = UIPopoverArrowDirectionUp;
popPresenter.delegate = self;
popPresenter.sourceView = sender;
popPresenter.sourceRect = [sender bounds];
[self presentViewController:testVC animated:YES completion:^{}];
}
#pragma mark protocol (UIPopoverPresentationControllerDelegate)
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection
{
return UIModalPresentationNone;
}
@end
答案 3 :(得分:-1)