【问题标题】:How to present a popover using Storyboards only如何仅使用 Storyboards 呈现弹出框
【发布时间】:2015-02-05 15:18:55
【问题描述】:

我使用的是 XCode 6.1,但没有“弹出框”选项。我的配置有问题吗?这是正常的吗?他们删除了吗?我现在如何呈现弹出框?

这是用于 iPhone 开发,而不是 iPad。

【问题讨论】:

  • 我认为你必须将模拟指标更改为 iPad
  • 什么意思?我正在为 iPhone 开发。

标签: ios objective-c iphone swift storyboard


【解决方案1】:

来自文档 (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverController_class/index.html)

Popover 控制器专用于 iPad 设备。 尝试在其他设备上创建一个会导致异常。

如果您想使用弹出框之类的东西,则必须使用第三方代码。

【讨论】:

  • iphone 有其他选择吗?我只想在同一屏幕上的任何地方弹出一个 uipicker。我必须使用新的 UIViewController 吗?
  • 你可以添加一个 UIPickerView 到你的 ViewController 的主视图中。也许从底部或其他东西在屏幕上对其进行动画处理。
【解决方案2】:

您可以随心所欲地创建一个 UIView,并让选择器启动它以类似于相同的感觉:

以这张照片为例:

视图可以这样制作,注意 filterView 已添加到超级视图中,因为我已将视图的 userInteractionEnabled 设置为 NO,因此用户必须在继续使用视图之前选择一个选项,但是,它是可选的,我只是指出来:

  self.view.userInteractionEnabled = NO;
  [self.navigationItem.rightBarButtonItem setEnabled:NO];
self.filterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
float X_CO = (self.view.superview.bounds.size.width - self.filterView.frame.size.width)/2;
float Y_CO = (self.view.superview.bounds.size.width - self.filterView.frame.size.height)/2;
[self.filterView setFrame:CGRectMake(X_CO, Y_CO + 75, 150, 150)];
self.filterView.backgroundColor = [UIColor whiteColor];
self.filterView.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.filterView.layer.borderWidth = 1.0f;
self.filterView.layer.cornerRadius = 8.0f;
self.filterView.layer.shadowColor = [UIColor blackColor].CGColor;
self.filterView.layer.shadowOpacity = 0.5f;
self.filterView.layer.shadowOffset = CGSizeMake(0, 1);
self.filterView.layer.masksToBounds = NO;

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 50)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = @"Select Filter Option:";
titleLabel.font = [UIFont fontWithName:@"Eurostile" size:13];
titleLabel.textColor = [UIColor colorWithRed:48/255 green:131/255 blue:251/255 alpha:1];
[self.filterView addSubview:titleLabel];

self.azButton = [[UIButton alloc] initWithFrame:CGRectMake(10,self.filterView.frame.origin.y/3.5,30, 30)];
[self.azButton setBackgroundColor:[UIColor whiteColor]];
[self.azButton addTarget:self action:@selector(filterButton:) forControlEvents:UIControlEventTouchUpInside];
self.azButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.azButton.layer.cornerRadius = 15.0f;
self.azButton.layer.borderWidth = 1.0f;
[self.filterView addSubview:self.azButton];

UILabel *azLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.azButton.frame.origin.x + 40, self.azButton.frame.origin.y, 95, 30)];
azLabel.textAlignment = NSTextAlignmentLeft;
azLabel.text = @"A-Z Sort";
azLabel.textColor = [UIColor blackColor];
azLabel.font = [UIFont fontWithName:@"Eurostile" size:20];
[self.filterView addSubview:azLabel];

self.zaButton = [[UIButton alloc] initWithFrame:CGRectMake(10, self.azButton.frame.origin.y + 40,30, 30)];
[self.zaButton setBackgroundColor:[UIColor whiteColor]];
[self.zaButton addTarget:self action:@selector(filterButton:) forControlEvents:UIControlEventTouchUpInside];
self.zaButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.zaButton.layer.cornerRadius = 15.0f;
self.zaButton.layer.borderWidth = 1.0f;
[self.filterView addSubview:self.zaButton];

UILabel *zaLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.azButton.frame.origin.x + 40, self.zaButton.frame.origin.y, 95, 30)];
zaLabel.textAlignment = NSTextAlignmentLeft;
zaLabel.text = @"Z-A Sort";
zaLabel.textColor = [UIColor blackColor];
zaLabel.font = [UIFont fontWithName:@"Eurostile" size:20];
[self.filterView addSubview:zaLabel ];

[self.view.superview addSubview:self.filterView];

您可以添加背景图像以产生弹出视图的效果。

然后在用户触摸点调用它。

只是一个例子:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass: UITableView.class]) {
     //your touch was in a UITableView ... do whatever you have to do
}
}

然后对 touchesEnded 做同样的事情:但是还有很多其他的方法可以做到这一点。 (即,找到确切的联系点等,但这取决于你。

正如 Duncan C 指出的那样,还有其他免费可用的框架可供使用。

当然,您总是可以在故事板或 xib 中创建它,有很多选择

【讨论】:

    【解决方案3】:

    正如 Daniel 在他的帖子中所说,UIPopoverController 对象仅适用于 iPad。

    有一些第三方库可以让您在 iPhone 上获得相同的外观和感觉。我没有具体的可以指点您,但请查看 Github。

    【讨论】:

      【解决方案4】:

      当你按住 Control 并拖动到你的第二个 VC 时,你应该会看到这个:

      当您选择“popover presentation”时,生成的 segue 应如下所示:

      这个选项绝对应该适用于 iPhone,而且您应该会看到它。如果没有,您的项目设置可能有问题。

      需要注意的是,这与使用 UIPopoverController 不同。相反,它调用UIPopoverPresentationController,这是 iOS 8 中的新功能(我相信)。在 iPhone 中工作,不需要 3rd 方框架。我目前正在一个 iPhone 项目中成功使用它,所以我知道它可以工作。

      我相信@Daniel T. 的回答虽然在早期版本中是正确的,但已被 UIPopoverPresentationController 废弃。

      【讨论】:

        猜你喜欢
        • 2012-02-05
        • 2013-06-06
        • 2019-02-26
        • 1970-01-01
        • 1970-01-01
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        • 2017-11-12
        相关资源
        最近更新 更多