文本字段不向上移动的UIAlertView [英] UIAlertView with text fields not moving up

查看:83
本文介绍了文本字段不向上移动的UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个提示,要求输入用户名和密码.

I want to display a prompt to get a user's username and password.

我这样做是通过显示UIAlertView并将UITextFields添加为它的子视图来实现的.

I am doing this by displaying a UIAlertView and adding UITextFields as subviews to it.

最近,我将项目移至XCode 4.2,并更新了所有项目设置.我的基本SDK设置为iOS 5.0,部署目标设置为4.0.

Recently I moved my project over to XCode 4.2 and I updated all my project settings. My base SDK is set to iOS 5.0 and my deployment target is set to 4.0.

然后我注意到它仅是为armv7架构而构建的,因此我也将其更改为包括armv6,因为我也希望支持iPhone 3G.

I then noticed that it was only being built for armv7 architectures and so I changed that to include armv6 as well since I would like to support the iPhone 3G as well.

执行完此操作后,我注意到UIAlertView并没有像以前那样向上移动,现在它被显示时的键盘覆盖了.我在Apple文档中读到,您不应该将UIAlertViews子类化( http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html ),所以我更改了工作方式.这不能解决问题.

After doing this I noticed that the UIAlertView wasn't moving up as before and now it was being covered by the keyboard when it was displayed. I read in the Apple documentation that you shouldn't subclass UIAlertViews (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html) and so I changed the way I was doing things. This didn't solve the problem.

我注意到,通过将构建设置-仅构建活动架构"设置为是",我可以在手机(iPhone 4,armv7架构)上使用它.

I have noticed that I can get it to work on my phone (an iPhone 4, so armv7 architecture) by setting the Build Setting - "Build active architecture only" to YES.

我怀疑问题与尝试为armv6进行构建有关(实际上,从我试图为其构建的体系结构中删除了该问题可以使我的警报向上移动).

I suspect that the problem is something to do with trying to build for armv6 (and indeed removing this from the architectures I am trying to build for gives me alerts which move up correctly).

我想现在应该回答我的问题了……我正在努力理解为什么这是这样的.我读到某个地方(现在找不到链接),当在iOS 4及更高版本的UIAlertView中添加文本字段时,不需要手动将警报上移.由于我至少要为iOS 4进行构建,所以这两种架构都不应该都可以吗?

I suppose I should get to my question now... I'm struggling to understand why this is behaving the way it is. I read somewhere (can't find the link now) that you don't need to move the alert up manually when adding a text field to a UIAlertView in iOS 4 and above. Since I am building for at least iOS 4 shouldn't this work on both architectures?

此外,我该如何为armv6构建它,并且仍然具有可以正确向上移动的警报?

Also, how can I get it to build for armv6 and still have alerts which move up correctly?

也许我错了,但是我注意到我有两个手机实例,可以将其部署到该实例中.选择第一个并构建会给我一个应用程序,其中UIAlertViews不会在应有的时候向上移动,但是选择第二个将使其正常运行.将发布屏幕截图,但是我是新用户,所以我还没有必要的权限...

Maybe I was wrong but I have noticed that I have 2 instances of my phone which I can deploy the app to. Selecting the first one and building gives me an app where the UIAlertViews don't move up when they should, but selecting the second one makes it work properly. Would post a screenshot, but I'm a new user and so I don't have the permissions necessary yet...

推荐答案

Oki,

我设法提出了一个可行的解决方案,并将UIAlertView设置在正确的位置,但是我仍然不确定问题的根本原因是什么.

I've managed to come up with a solution which works and gets the UIAlertView to be in the correct position, but I'm still not entirely sure what the root cause of the problem is.

我通过响应UIAlertViewDelegate方法willPresentAlertView:并执行以下操作

I solved it by responding to the UIAlertViewDelegate method willPresentAlertView: and doing the following

- (void) willPresentAlertView: (UIAlertView *) alertView {

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

    if (keyWindow.center.y - alertView.center.y < 0.001f) {

        CGPoint center = [alertView center];
        center.y = center.y - 108.0f;
        [alertView setCenter:center];  
    }
}

它检查UIAlertView是否在屏幕中央并向上移动(例如,如果操作系统尚未计算出它的正确位置).注意:如果您希望设备同时适用于纵向和横向,则需要添加一个检查,确定设备所处的特定方向,然后相应地调整警报向上移动的量.

It checks whether the UIAlertView is in the center of the screen and moves it up if it is (i.e. if the OS hasn't already calculated the correct position of it). Note: You would need to add a check for which specific orientation the device is in and then adjust the amount the alert is moved up accordingly if you wanted this to work for both portrait and landscape orientations.

这篇关于文本字段不向上移动的UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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