gpt4 book ai didi

ios - 当我从弹出的 View Controller 退出时,UIScrollView 向下移动

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:08 24 4
gpt4 key购买 nike

我对导航 Controller 和 2 个 View Controller 有疑问。当我弹出到其他 View Controller 时一切正常,但是当我返回到我的第一个 View 时它显示了一个导航栏下的大白色区域

在第二个 Controller 上,当我编辑 UITextField 时,键盘会自动显示和隐藏。当我从显示键盘的第二个 View Controller 返回时,这个问题就开始了。其他方式(不编辑 UITextField)我对第一个 Controller 没有这个问题。

我需要做什么?

第一个.m文件

#import "OTPTRecordViewController.h"
#import "OTPTAppDelegate.h"
#import "OTPTPractice.h"


@implementation OTPTRecordViewController
@synthesize scrollView, activeField;

- (void)viewDidLoad
{
[super viewDidLoad];
[self registerForKeyboardNotifications];

viewBackgroundTap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:viewBackgroundTap];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

UIEdgeInsets contentInsets = UIEdgeInsetsMake(-20.0, 0.0, 0.0, 0.0);
//I need it for correct view insets

scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}

#pragma mark Textfields&Keyboard management

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}

- (IBAction)backgroundTap:(id)sender
{
[nameTextField resignFirstResponder];
[lengthTextField resignFirstResponder];
}

- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

if (([holdOutTextField isEditing]) || ([cyclesTextField isEditing])) {
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+50.0, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}

CGRect aRect = self.view.frame;
//For scroll view moves
aRect.size.height -= kbSize.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
CGPoint origin = activeField.frame.origin;
origin.y -= scrollView.contentOffset.y;
if (!CGRectContainsPoint(aRect, origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-(aRect.size.height));
[scrollView setContentOffset:scrollPoint animated:YES];
}
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsMake(-20.0, 0.0, 0.0, 0.0);

scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
[activeField setSelectedTextRange:[activeField textRangeFromPosition:activeField.beginningOfDocument toPosition:activeField.endOfDocument]];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}

@end

第二个.m文件

#import "OTPTSaveViewController.h"
#import "OTPTAppDelegate.h"
#import "OTPTRecordViewController.h"

@interface OTPTSaveViewController () {
UIViewController *parentViewController;
}

@end

@implementation OTPTSaveViewController

@synthesize textField;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *viewBackgroundTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap:)];
viewBackgroundTap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:viewBackgroundTap];
parentViewController = [self presentingViewController];
}

- (IBAction)cancel:(id)sender {
[[self navigationController] popViewControllerAnimated:YES];
}

- (IBAction)save:(id)sender {
//some saving
}


- (BOOL)textFieldShouldReturn:(UITextField *)tf {
[tf resignFirstResponder];
return NO;
}

- (IBAction)backgroundTap:(id)sender
{
[textField resignFirstResponder];
}

@end

我认为这个问题是因为来自弹出 Controller 的键盘通知。但是当我添加

[self unregisterForKeyboardNotifications];

viewWillDisappear 和 make 方法

- (void) unregisterForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

对我没有影响

最佳答案

从 iOS 7 开始,UIViewController 添加了一个新属性...

@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets

根据 Apple Docs:

Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

如果您手动管理插图,您可能希望将此属性设置为 NO,因为这会干扰您尝试执行的操作。

关于ios - 当我从弹出的 View Controller 退出时,UIScrollView 向下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com