简体   繁体   English

在Interface Builder中为Iphone创建popover视图控制器

[英]Creating popover view controllers for Iphone in Interface Builder

How do I go about doing this? 我该怎么做呢? I know I have to give the popover a view controller that it constructs its view from, but I don't know how to construct this view in Interface Builder (like adding the labels and buttons and whatnot). 我知道我必须给popover一个视图控制器,它构建它的视图,但我不知道如何在Interface Builder中构建这个视图(比如添加标签和按钮等等)。

Do I just do it in the normal storyboard and have a storyboard that's randomly not connected to anything else, sitting off in the corner? 我是否只是在普通的故事板中进行操作,并且有一个随机地与其他任何东西无关的故事板,坐在角落里?

Do I create another storyboard? 我是否创建了另一个故事板?

Do I create a xib even though my project is all storyboard? 即使我的项目都是故事板,我也会创建一个xib吗?

Do I create it programatically? 我是以编程方式创建的吗?

Since You did not specified target device in your question, I gave you Ipad answer 由于你没有在你的问题中指定目标设备,我给你Ipad答案

IPAD: IPAD:

Go to your storyboard drag and drop a viewcontroller or tableviewcontroller its up to you. 转到您的故事板,将viewcontrollertableviewcontroller拖放到您的身上。 Then create a segue from desired viewcontroller on your storyboard to newly dragged/dropped viewcontroller . 然后在故事板上从所需的viewcontroller创建一个segue到新拖放的viewcontroller Choose your segue as popOver . 选择你的segue作为popOver

在此输入图像描述

在此输入图像描述

Make sure you choose an anchor in segue settings like above picture. 确保在segue设置中选择锚点,如上图所示。 Then you need to edit size of your popover . 然后,您需要编辑弹出窗口的大小。 If its a uiviewcontroller choose its view,if its a tableviewcontroller choose its tableview on left side of interface builder and edit its size. 如果它是一个uiviewcontroller选择自己的看法,如果其tableviewcontroller选择在界面生成器的左侧它的tableview和编辑它的大小。

First: 第一:

在此输入图像描述

Second: 第二:

在此输入图像描述

After that customize your popover view controller (dragged/dropped view controller) add button,label whatever you want. 之后,自定义您的弹出视图控制器(拖放视图控制器)添加按钮,标记您想要的任何内容。

If you are going to do additional things in your popover: Dont forget to create new file -> subclass of uiviewcontroller or uitableviewcontroller. 如果你打算在你的popover中做更多的事情:别忘了创建新文件 - > uiviewcontroller或uitableviewcontroller的子类。 Then associate it with your newly created viewcontroller on storyboard. 然后将它与故事板上新创建的viewcontroller关联。

在此输入图像描述

IPHONE: 苹果手机:

There are no Popover controllers in iphone iphone中没有Popover控制器

But you try to use a third party https://github.com/50pixels/FPPopover solution that mimics popover behaviors in iphone by using QuartzCore 但是你尝试使用QuartzCore第三方https://github.com/50pixels/FPPopover解决方案来模仿iphone中的QuartzCore行为

I would try following: 我会尝试以下:

First: 第一:

Again drag and drop a uiviewcontoller to your storyboard, then create a new file-> subclass of uiviewcontroller . 再次将uiviewcontoller拖放到故事板中,然后创建一个新文件 - > uiviewcontroller子类。

This new uiviewcontroller will sit in the corner in your storyboard 这个新的uiviewcontroller将位于故事板的角落

Then associate your uiviewcontroller in your storyboard with new Created file , lets say you created a new file name with YourViewController . 然后将故事板中的uiviewcontroller与新创建的文件相关联,假设您使用YourViewController创建了一个新文件名。 Give your view a storyboard id and choose its class name. 为您的视图提供一个storyboard id并选择其类名。

在此输入图像描述

-(IBAction)buttonClicked:(UIButton*)okButton
{
    //the view controller you want to present as popover
    YourViewController *controller = [[YourViewController alloc] init]; 

    //if [[YourViewController alloc] init]; doesn't work try this
   // UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                     bundle:nil];
    //YourViewController *controller = [sb instantiateViewControllerWithIdentifier:@"YourViewController"]; 

    //our popover
    FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller]; 

    //the popover will be presented from the okButton view 
    [popover presentPopoverFromView:okButton]; 

    //no release (ARC enable)
    //[controller release];
}

Note: I havent used that FPPopover before so dont know how to arrange screen size but there must be further explanation in their documents just read it. 注意:我之前没有使用过FPPopover因此不知道如何安排屏幕尺寸,但是在他们的文档中必须进一步解释才能阅读它。

I think the better approach for the iPhone device is, that you create a View Controller whose View has the background colour set to clear. 我认为iPhone设备的更好方法是,创建一个View控件,其View的背景颜色设置为clear。 Then in this view you just add a smaller sized view which will give the appearance of a popup. 然后在此视图中,您只需添加一个较小的视图,该视图将显示弹出窗口的外观。

Presenting this view controller will give the appearance of a popup. 呈现此视图控制器将给出弹出窗口的外观。 This is the approach I took. 这是我采取的方法。 If the user taps on an area on the parent view that is outside the bounds of the smaller view, you can dismiss the view controller. 如果用户点击父视图上不在较小视图范围之外的区域,则可以关闭视图控制器。

iPhone solution iPhone解决方案

Since iOS 8.0 is is possible to realize this for iPhone apps too! 由于iOS 8.0也可以为iPhone应用程序实现这一点!

The target view controller (the one, which should appear in a popover) needs to conform to the ' UIPopoverPresentationControllerDelegate ' protocol and has to implement the message ' adaptivePresentationStyleForPresentationController:traitCollection: '. 目标视图控制器(应该出现在弹出框中的那个 )需要符合' UIPopoverPresentationControllerDelegate '协议,并且必须实现消息' adaptivePresentationStyleForPresentationController:traitCollection: '。 In addition to this its ' modalPresentationStyle ' should be ' UIModalPresentationPopover ' and the controller should be his own ' UIPopoverPresentationController ' delegate. 除此之外,它的' modalPresentationStyle '应该是' UIModalPresentationPopover ',控制器应该是他自己的' UIPopoverPresentationController '委托。

After preparing the view controller this way, just create a storyboard segue (type: 'Present As Popover') from your UINavigationBarItem (or anything else) to the target view controller. 在以这种方式准备视图控制器之后,只需从UINavigationBarItem(或其他任何内容)创建一个storyboard segue(类型:'Present As Popover')到目标视图控制器。

在此输入图像描述

Implementation: 执行:

@implementation TestViewController

/*
 initWithCoder:

 */
- (instancetype)initWithCoder:(NSCoder *)pDecoder {
    //FLog;

    if (self = [super initWithCoder:pDecoder]) {

        self.modalPresentationStyle = UIModalPresentationPopover;
        self.popoverPresentationController.delegate = self;
    }
    return self;
}


#pragma mark - POPOVER PRESENTATION CONTROLLER DELEGATE

/*
 adaptivePresentationStyleForPresentationController:traitCollection:

 */
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)pController
                                                               traitCollection:(UITraitCollection *)pTraitCollection {
    //FLog;

    return UIModalPresentationNone;
}

// ...

@end

Of course the delegate protocol can be implemented in some other class also, but this way is the easiest one :-) 当然委托协议也可以在其他类中实现,但这种方式最简单:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM