如何移动UIAlertView中的按钮为插入的UITextField留出空间? [英] How to move the buttons in a UIAlertView to make room for an inserted UITextField?

查看:117
本文介绍了如何移动UIAlertView中的按钮为插入的UITextField留出空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯。也许这个问题应该标题为CocoaTouch中调用的默认用户输入对话框视图是什么?我意识到,我可以创建一个完全是我想要的视图,并将其包装在一个视图控制器和presentModalView - 但我希望有一个标准的,普通的用户输入对话框视图,与可可粉接触。 输入您的姓名,输入要搜索的文本等是非常常见的事情!



反正...这里是我最初问的问题: / p>

此代码:

  UIAlertView * find = [[UIAlertView alloc]在里面]; 
[find setDelegate:self];
[find setTitle:@Find];

[find addButtonWithTitle:@Cancel];
[find addButtonWithTitle:@Find& Bring];
[find addButtonWithTitle:@Find& Go];
[find addButtonWithTitle:@Go To Next];

[find addSubview:_findText];

CGRect frm = find.frame;
int height = frm.size.height + _findText.frame.size.height + 100; //注意,即使100没有效果。
[find setFrame:CGRectMake(frm.origin.x,frm.origin.y,frm.size.width,height)];

[find setNeedsLayout];
[find show];
[find release];

产生此警报视图:



img src =http://www.publicplayground.com/IMGs/Misc/FindAlert.pngalt =查找提醒>



(我开始使用代码来自 this问题由emi1Faber ,它的工作原理像广告;但是,正如我在我的评论,取消按钮覆盖文本字段。)



我如何重新整理一切,使文本字段适合正确? [findAlert setNeedsLayout]似乎没有做任何事情,即使我[findAlert setFrame:tallerFrame]。提示?



谢谢!

解决方案

方式)向下移动文本视图是添加消息

  [find setMessage:@\\\
];

此外,框架未生效的原因是 -show 设置框架并在开始动画之前创建视图层次结构。



完整示例:

  //创建警报
UIAlertView * av = [UIAlertView new];
av.title = @Find;
//添加按钮
[av addButtonWithTitle:@Cancel];
[av addButtonWithTitle:@Find& Bring];
[av addButtonWithTitle:@Find& Go];
[av addButtonWithTitle:@Go to Next];
//为文本视图创建空间
av.message = @\\\
;
//让警报视图创建它的视图后视图,设置其框架并开始退回动画
[av show];
//调整帧
CGRect frame = av.frame;
frame.origin.y - = 100.0f;
av.frame = frame;
//添加文本字段
UITextField * text = [[UITextField alloc] initWithFrame:CGRectMake(20.0,45.0,245.0,25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[av addSubview:text];
[text becomeFirstResponder];

注意:您也可以修改UIAlertView的子视图,但由于Apple已经更改了UIAlertView布局一次你应该在设置新的类之前检查它们的类描述和框架与已知的值。你甚至可以得到这样的东西:




[EDIT] Hmm. Perhaps this question should be titled "what is the default user-input dialog view called in CocoaTouch?" I realize that I can create an entire view that is exactly what I want, and wrap it in a view controller and presentModalView -- but I was sort of hoping that there was a standard, normal user-input "dialog" view that came-with Cocoa-touch. "Enter your name", "enter text to search", etc., are VERY common things!

Anyway... here's the question as I originally asked it:

This code:

UIAlertView* find = [[UIAlertView alloc] init];
[find setDelegate:self];
[find setTitle:@"Find"];

[find addButtonWithTitle:@"Cancel"];
[find addButtonWithTitle:@"Find & Bring"];
[find addButtonWithTitle:@"Find & Go"];
[find addButtonWithTitle:@"Go To Next"];

[find addSubview:_findText];

CGRect frm = find.frame;
int height = frm.size.height + _findText.frame.size.height + 100; // note how even 100 has no effect.
[find setFrame:CGRectMake(frm.origin.x, frm.origin.y, frm.size.width, height)];

[find setNeedsLayout];
[find show];
[find release];

Produces this Alert view:

(I started with the code from this question by emi1Faber, and it works as advertised; however, as I state in my comment, the cancel button overlays the text field.)

How do I reshuffle everything to make the text field fit properly? [findAlert setNeedsLayout] doesn't seem to do anything, even after I [findAlert setFrame:tallerFrame]. Hints?

Thanks!

解决方案

The simplest (and most proper way) to move the text view down is to add a message

[find setMessage:@"\n"];

Also, the reason your frame isn't taking effect is that -show sets the frame and creates the view hierarchy before starting the animation. You should also make the text view the first responder so the keyboard pops up.

Full example:

// Create Alert
UIAlertView* av = [UIAlertView new];
av.title = @"Find";
// Add Buttons
[av addButtonWithTitle:@"Cancel"];
[av addButtonWithTitle:@"Find & Bring"];
[av addButtonWithTitle:@"Find & Go"];
[av addButtonWithTitle:@"Go to Next"];
// Make Space for Text View
av.message = @"\n";
// Have Alert View create its view heirarchy, set its frame and begin bounce animation
[av show];
// Adjust the frame
CGRect frame = av.frame;
frame.origin.y -= 100.0f;
av.frame = frame;
// Add Text Field
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[av addSubview:text];
[text becomeFirstResponder];

Note: You can also modify the subviews of UIAlertView, but since Apple has already changed the UIAlertView layout once you should check their class descriptions and frames against known values before setting new ones. You can even get something like this:

这篇关于如何移动UIAlertView中的按钮为插入的UITextField留出空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆