首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIPopover行为

UIPopover行为
EN

Stack Overflow用户
提问于 2012-07-28 04:41:25
回答 2查看 155关注 0票数 1

我有一个弹出式视图(计算器),每当开始编辑文本字段时都会显示该视图。调用display方法的代码和display方法本身如下所示。

代码语言:javascript
复制
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    //the background color may have been yellow if someone tried to submit the form with a blank field
    textField.backgroundColor = [UIColor whiteColor];

    sender = @"text field";

    [self displayCalculator:textField.frame];

    return YES;
}

显示视图的方法为:

代码语言:javascript
复制
-(IBAction)displayCalculator:(CGRect)rect{

    calculator = [[CalculatorViewController alloc] initWithNibName:@"CalculatorViewController" bundle:nil];
    popoverController = [[[UIPopoverController alloc] initWithContentViewController:calculator] retain];

    [popoverController setPopoverContentSize:CGSizeMake(273.0f, 100.0f)];

    [popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

我的问题是:

1)如何让popover留下来?我希望用户能够单击文本字段(首先显示弹出窗口的文本字段),但当他们这样做时,弹出窗口就消失了。

2)弹出窗口有时会以阻止文本字段的方式出现,有没有我可以控制弹出窗口显示位置的方法?我当前正在传递文本字段的框架,但这似乎不起作用

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-28 05:14:55

检查文档以查看是否有解决所需任务或特性的方法或属性(UIPopoverController)总是很好的做法

对于您的第一个问题,您应该看看passthroughViews属性:

passthroughViews

用户可以在弹出窗口可见时与之交互的视图数组。@property (非原子,复制) NSArray *透视图讨论

当弹出按钮处于活动状态时,通常会禁用与其他视图的交互,直到取消弹出按钮为止。将视图数组分配给此属性允许相应的视图处理弹出窗口之外的点击。

对于第二个问题(覆盖文本区域),您可以对textField.frame进行偏移,以便为popoverController定义一个新的CGRect作为其锚。

代码语言:javascript
复制
CGRect targetRect = CGRectOffset(textField.frame, n, n);
票数 2
EN

Stack Overflow用户

发布于 2012-07-28 05:05:55

(问题1)在您的displayCalculator方法中,您需要有一种方法来检查弹出窗口是否已经显示。从现在开始,每次textField更新时,您都会重新绘制弹出窗口。您已经将textFieldDelegate调用更改为textFieldDidBeginEditing

试试这个:

代码语言:javascript
复制
-(BOOL)textFieldDidBeginEditing:(UITextField *)textField {

    //the background color may have been yellow if someone tried to submit the form with a blank field
    textField.backgroundColor = [UIColor whiteColor];

    sender = @"text field";

    [self displayCalculator:textField.frame];

    return YES;
}

-(IBAction)displayCalculator:(CGRect)rect{

//We don't want to continually create a new instance of popoverController. So only if it is nil we create one. 
if (popoverController == nil)
    calculator = [[CalculatorViewController alloc] initWithNibName:@"CalculatorViewController" bundle:nil];
    popoverController = [[[UIPopoverController alloc] initWithContentViewController:calculator] retain];

    [popoverController setPopoverContentSize:CGSizeMake(273.0f, 100.0f)];
}

//Check to make sure it isn't already showing. If it's not, then we show it. 
if (!popoverController.popoverVisible) {
    [popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

}

编辑

正如skinny指出的(我应该提到的)。当你在弹出框外触摸时,弹出框就会消失。这就是将textFieldDelegate更改为textFieldDidBeginEditing的原因。

Here is a good tutorial that might be able to help you.

If all else fails just create your own popover.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11695366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档