2012-10-02 37 views

回答

2

首先创建一个视图来保存整个事情。 然后,使其出现下实现代码如下。新增的文本框到工具栏

UIView *placeholderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 400, 440)]; 
    UITableView *tv=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 400, 400)]; 
    [placeholderView addSubview:tv]; 
    UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 400, 400, 40)]; 
    [placeholderView addSubview:toolBar]; 
5

使用UIViewController子类来代替的UITableViewController子类中添加一个UITableView和UIToolbar编程方式设置框。

它应该是这样的:

@interface ChatViewController : UIViewController 
@end 


#import "ChatViewController.h" 

@interface ChatViewController() <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> 
@property (nonatomic, strong) UITableView *tableView; 
@property (nonatomic, strong) UIToolbar *toolbar; 
@property (nonatomic, strong) UITextField *textField; 
@end 

@implementation ChatViewController 

-(UITableView *)tableView 
{ 
    if (!_tableView) { 
     _tableView = [UITableView alloc] init]; 
     CGRect frame = self.view.bounds; 
     frame.size.height = frame.size.height - 44; 
     _tableView.frame = frame; 
     _tableView.delegate = self; 
     _tableView.dataSource = self; 
    } 
    return _tableView; 
} 

-(UIToolbar *)toolbar 
{ 
    if (!_toolbar) { 
     _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.tableView.frame.size.height,self.view.frame.size.width, 44)]; 
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,_toolbar.frame.size.width -20)]; 
    self.textField.delegate = self; 
    UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:self.textField]; 
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                        target:nil 
                        action:nil]; 
    // You'll need to add a button to send you text 
    _toolbar.items = [NSArray arrayWithObjects:flexibleSpace, textFieldItem, flexibleSpace, nil]; 
    } 
    return _toolbar; 
} 

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.view addSubview:self.tableView]; 
    [self.view addSubview:self.toolbar]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHideOrShow:) 
               name:UIKeyboardWillHideNotification 
               object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHideOrShow:) 
               name:UIKeyboardWillShowNotification 
               object:nil]; 
} 

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


- (void)keyboardWillHideOrShow:(NSNotification *)note 
{ 
    NSDictionary *userInfo = note.userInfo; 
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; 

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    CGRect keyboardFrameForToolbar = [self.toolbar.superview convertRect:keyboardFrame fromView:nil]; 
    CGRect keyboardFrameForTableView = [self.tableView.superview convertRect:keyboardFrame fromView:nil]; 

    CGRect newToolbarFrame = self.toolbar.frame; 
    newToolbarFrame.origin.y = keyboardFrameForToolbar.origin.y - newToolbarFrame.size.height; 

    CGRect newTableViewFrame = self.tableView.frame; 
    newTableViewFrame.size.height = keyboardFrameForTableView.origin.y - newToolbarFrame.size.height; 

    [UIView animateWithDuration:duration 
          delay:0 
         options:UIViewAnimationOptionBeginFromCurrentState | curve 
        animations:^{self.toolbar.frame = newToolbarFrame; 
         self.tableView.frame =newTableViewFrame;} 
        completion:nil]; 
} 

这将处理布局的意见和动画键盘的外观。您需要处理表视图和文本字段的委托和数据源方法。

+0

好的答案,看起来很有前途,但我猜想只要正确设置表格视图和工具栏尺寸,就没有任何使用“UITableViewController”的缺点。我想知道为什么你建议不要使用'UITableViewController'? –

+0

如果您尝试将子视图添加到其主视图(这是一个表视图),那么'UITableViewController'可能会非常棘手。 – Moxy

26

我刚刚找到了一个更好的把戏!

  1. 确保没有导航栏(从你的尝试失败后)
  2. 拖放“栏按钮项目”(Xcode中会奇迹般地将其用于您在底部)
  3. 注意:如果你现在运行应用程序,你将看不到任何东西! (所以保持读取)
  4. 添加以下代码行viewDidLoad中下:

self.navigationController.toolbarHidden = NO;

完成!

+0

这正是我期待的! –

+0

你钉了它!太感谢了。我原来是这样,然后我把它删除了,我忘了我怎么把它放回去。 – pqsk

+0

它的工作原理!此外,请确保UIBarButtonItem上的色调颜色不是白色的... – rjobidon

1

这在某些方面并不是很好,但我通过在其他View Controller上使用Container View来解决此问题。

Storyboard screenshot

中级VC表VC赛格瑞是“嵌入”

I“上导航控制器工具栏”溶液来代替这一点,因为我添加这对现有应用设计〜 15个屏幕,我不确定如何在不同的屏幕上配置不同的工具栏。

+0

我同意这不是很好,但它只是绕过一个明显的漏洞给IOS,因此您不能将UIToolbar添加到UIViewController,而无需首先将该UIVC嵌入到UINavigationController的。在我的情况下,由于UINC的其他“设计特征”,我无法将我的UIVC嵌入到UINC中。 –