在 iOS 8.1 之后,在所有设备上,当点击 alertview 取消按钮时,ScrollView 向上并且不滚动
Posted
技术标签:
【中文标题】在 iOS 8.1 之后,在所有设备上,当点击 alertview 取消按钮时,ScrollView 向上并且不滚动【英文标题】:After iOS 8.1, on all devices ScrollView goes upwards and not scrolling when tapping on alertview cancel button 【发布时间】:2015-08-18 22:10:33 【问题描述】:我有一个登录屏幕,其中有两个用于用户名和密码的文本字段。当电子邮件不正确时,我向下移动键盘并向用户显示适当的警报。当我单击警报视图的取消按钮时,警报视图消失并且滚动视图向上。此问题仅出现在 ios 8.1 之后的设备上。在 iOS 8.1 及更早版本上,它运行良好。不明白具体原因是什么?下面是代码
-(BOOL)textFieldShouldReturn:(UITextField *)textField
if (txtActiveField == txtUsername)
[txtPassword becomeFirstResponder];
else if (txtActiveField == txtPassword)
[txtPassword resignFirstResponder];
//[self loginViaEmail:self];
[self performSelector:@selector(loginViaEmail:) withObject:btnLogin afterDelay:0.0];
return YES;
-(IBAction)loginViaEmail:(id)sender
//[txtActiveField resignFirstResponder];
if ([[txtUsername.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]] length] == 0)
[AppDelegate showWithTitle:title message:@"Please enter Username/Email for your account"];
else if ([txtPassword.text length] < 6)
if([[txtPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]] length] == 0)
[AppDelegate showWithTitle:title message:@"Please enter Password"];
else
[AppDelegate showWithTitle:title message:@"Password contain minimum 6 characters"];
#pragma mark Show alert with message
+(void)showWithTitle:(NSString *)title message:(NSString *)msg
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
【问题讨论】:
【参考方案1】:我认为这是警报视图的问题。从 iOS 8 开始,AlertViewController 代替了警报视图。尝试使用以下代码,它在 iOS 8.4 及以下版本上运行良好
if (IS_OS_8_OR_LATER)
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
];
[alertVC addAction:cancelAction];
[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^
];
else
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
【讨论】:
以上是关于在 iOS 8.1 之后,在所有设备上,当点击 alertview 取消按钮时,ScrollView 向上并且不滚动的主要内容,如果未能解决你的问题,请参考以下文章
Windows Phone 8.1 应用程序未完全在 Windows 10 设备上运行
Android 8.1(API 27) - 重启后键盘未在Kiosk模式应用中显示