【问题标题】:Embedding a UITableView as a container view within a UIViewController将 UITableView 作为容器视图嵌入到 UIViewController 中
【发布时间】:2016-08-10 16:05:16
【问题描述】:

我想添加一个文本字段和发送按钮,该按钮粘贴在 uitableview 的底部,类似于聊天应用程序。我在将 UITableView 作为容器视图嵌入 UIViewController 时遇到了 cmets。

但是,他们似乎缺乏如何实现这一目标的示例。更具体的细节包括在哪里添加文本字段/按钮以及在键盘出现时向上移动文本字段等。谢谢!

【问题讨论】:

  • 请参阅Floating button on UITableView. 上的这篇最新帖子,这可能会有所帮助。它使用UITableViewController 而不是UIViewController 虽然它适合您的要求。

标签: ios swift uitableview uiviewcontroller uicontainerview


【解决方案1】:

使用情节提要按照步骤进行

1) drag a uiviewcontroller from the object library.
2) drag and drop the textfield and button and place it at the position you want
3) drag and drop a container view.
4) delete the default uiviewcontroller comes with the container view
5) drag a uitableviewcontroller and make a segue and the segue should be embedsegue.

对于键盘处理,您可以使用 IQKeyboardManagerhttps://github.com/hackiftekhar/IQKeyboardManager

【讨论】:

    【解决方案2】:

    很简单的聊天模型代码,大家可以看看:

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property UIView* containerView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _containerView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-30)];
    UITextField* tfInput = [[UITextField alloc] initWithFrame:CGRectMake(0, tableView.frame.size.height, [UIScreen mainScreen].bounds.size.width-50, 30)];
    tfInput.backgroundColor = [UIColor grayColor];
    UIButton* btnSend = [[UIButton alloc] initWithFrame:CGRectMake(tfInput.frame.size.width, tfInput.frame.origin.y, 50, 30)];
    [btnSend setTitle:@"Send" forState:UIControlStateNormal];
    [btnSend setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnSend addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [_containerView addSubview:tableView];
    [_containerView addSubview:tfInput];
    [_containerView addSubview:btnSend];
    [self.view addSubview:_containerView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    - (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    float keyboardHeight = [aValue CGRectValue].size.height;
    //Resize the container
    _containerView.frame = CGRectMake(0,  - keyboardHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }
    
    -(void)btnClicked{
    [self.view endEditing:YES];
    }
    
    - (void)keyboardWillHide:(NSNotification *)notification {
    _containerView.frame = [UIScreen mainScreen].bounds;
    }
    
    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    
    @end
    

    这是使用故事板的截图:

    【讨论】:

    • 谢谢!有没有办法使它适用于通过情节提要在 viewcontroller 中作为容器视图嵌入的 tableview(不是像上面的示例那样以编程方式创建)?
    • 和编程一样,拖一个UIView作为容器,然后把tableview、textfield、button一个一个拖到容器中,最后链接到ViewContoller.h作为outlet(对于button你可以使用一个动作)。关于键盘事件,你必须使用代码来完成它,如上所述。
    • 顺便说一句,我建议你使用代码来创建你的应用程序,特别是对于复杂的 UI,否则你会遇到很多问题。
    猜你喜欢
    • 2016-12-10
    • 2015-11-17
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2011-03-03
    相关资源
    最近更新 更多