1

我是IOS开发的新手。我想创建如图所示的视图。

我还希望当用户在文本字段中写入并且键盘出现 TextField 向上滚动到键盘时,应该隐藏在键盘后面。

在此处输入图像描述 谢谢,请帮忙

4

3 回答 3

1

您可能希望在页脚内添加文本字段和按钮。您可以使用键盘通知来移动视图偏移量,以便在用户键入键盘时可以看到您的文本字段。

页脚:*我假设文本字段和发送按钮是 ivars。

// add footer to table
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 85.0f)];
if(yourTextField ==nil){
     yourTextField = [[UITextField alloc] initWithFrame:CGRectMake(5,5,200,35)];
}
if(yourSendButton ==nil){
     yourSendButton = [[UIButton alloc] initWithFrame:CGRectMake(225,5,75,35)];
     yourSendButton addTarget:self action:@selector(sendBtnClicked) forControlEvents:UIControlEventTouchUpInside];
}


[footerView addSubview:yourTextField];
[footerView addSubview:yourSendButton];
[tableView setTableFooterView:footerView];

下面是一些用于移动键盘视图的示例代码:

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSValue *keyboardFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = [[self view] convertRect:[keyboardFrameValue CGRectValue] fromView:nil];
    NSNumber *duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[duration floatValue] animations:^{
        [scrollView setFrame:CGRectMake(scrollView.frame.origin.x,
                                        scrollView.frame.origin.y, 
                                        scrollView.frame.size.width, 
                                        keyboardFrame.origin.y - scrollView.frame.origin.y)];

    }];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    NSNumber *duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[duration floatValue] animations:^{
        [scrollView setFrame:CGRectMake(scrollView.frame.origin.x,
                                        scrollView.frame.origin.y, 
                                        scrollView.frame.size.width, 
                                        self.view.frame.size.height - (iPhone ? 44.0f : 0.0f))];
    }];
}
于 2012-12-11T15:33:18.743 回答
1

您将需要实现 UITableView 的委托方法:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

将文本字段添加到页脚的示例如下所示:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
  UIView* footer = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
  footer.backgroundColor = [UIColor whiteColor];

  UITextField* field = [[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
  [field setBorderStyle:UITextBorderStyleBezel];
  [footer addSubview:field];
  return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
  return 44;
}  

要让键盘自动正确响应 - 您只需要在完成后退出文本字段。

于 2012-12-11T15:47:19.007 回答
1

您不能使用UITableViewController标准视图控制器并在控制器中实现UITableViewDelegateUITableViewDataSource实现必要的委托方法,例如cellForRowAtIndexPath.

这将允许您拥有一个表格视图并像使用 a 一样控制它,UITableViewController并拥有您希望的任何其他界面元素。

于 2012-12-11T15:56:40.017 回答