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

查看:21
本文介绍了带有文本字段的 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?

也许我错了,但我注意到我的手机有 2 个实例,我可以将应用程序部署到其中.选择第一个并构建为我提供了一个应用程序,其中 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天全站免登陆