인터페이스에 textfield 포함 - 키보드 바운드

4530 단어
시나리오 1:
표시도에서 키보드가 튀어나올 때,tableView의 프레임을 변경하면, 테이블 보기는textfield를 표시할 위치에 자동으로 배치합니다.
코드:
     
4
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(visiblekeybordAction:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(hidekeybordAction:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320,568) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

#pragma mark UITableView Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 35;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell"];
    
    NDTCell *cell = [(UITableView *)tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = (NDTCell *)[[NDTCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.field.delegate = self;
    cell.field.text = [NSString stringWithFormat:@"%d",indexPath.row];
    return cell;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

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

#pragma mark - NSNotificationCenter
-(void)visiblekeybordAction:(NSNotification *)notification
{
    CGRect keyBoardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.tableView.frame = CGRectMake(0, 0,320 , 568 - keyBoardFrame.size.width);
}

-(void)hidekeybordAction:(NSNotificationCenter *)notification
{
    self.tableView.frame = CGRectMake(0,0,320,568);
}
방안2: UIScrollview를 직접 사용
결론:
scrollview는tableview와 비슷한 기능을 가지고 있는데 효과가 같다는 것을 알 수 있듯이tableView가 이 기능을 가진 이유는 계승자uiscrollview이다
코드는 다음과 같습니다.
  
4
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(visiblekeybordAction:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(hidekeybordAction:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize  = CGSizeMake(320, 80 * 35);
    self.scrollView.delegate = self;
    
    for (int m = 0; m < 35; m++)
    {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, m * 80, 320, 80)];
        textField.delegate = self;
        textField.backgroundColor = [UIColor greenColor];
        textField.text = [NSString stringWithFormat:@"%d",m];
        [self.scrollView addSubview:textField];
    }
    
    
}

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


-(void)visiblekeybordAction:(NSNotification *)notification
{
    CGRect keyBoardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.scrollView.frame = CGRectMake(0, 0,320 , 568 - keyBoardFrame.size.width);
}

-(void)hidekeybordAction:(NSNotificationCenter *)notification
{
    self.scrollView.frame = CGRectMake(0,0,320,568);
}
시나리오3:
직접view로 애니메이션을 만들고 이런 방법을 추천하지 않으며 적용 범위가 낮고 사용자 체험이 떨어진다.코드 정책

좋은 웹페이지 즐겨찾기