인터페이스에 textfield 포함 - 키보드 바운드
표시도에서 키보드가 튀어나올 때,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로 애니메이션을 만들고 이런 방법을 추천하지 않으며 적용 범위가 낮고 사용자 체험이 떨어진다.코드 정책
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.