如何在桌面视图的底部添加一个带有uitextfield的工具栏的Programmaticaly?就像聊天或短信应用程序..提前谢谢你..如何将工具栏添加到UITableView的底部obj-c/ios/xcode
5
A
回答
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
相关问题
- 1. 如何将工具栏添加到NSTableView的底部?
- 2. 如何将UISlider添加到底部工具栏?
- 3. 添加MPVolumeView至底部工具栏
- 4. 将按钮添加到工具栏底部
- 5. 将工具栏添加到部分
- 6. 如何将CheckBox添加到工具栏?
- 7. 如何将textField添加到工具栏
- 8. 如何在html页面中添加底部工具栏
- 9. 将搜索栏添加到UITableView顶部
- 10. 如何在工具栏的底部定位一个工具栏?
- 11. 如何将自定义单元添加到UITableView的底部?
- 12. 如何将子视图添加到UITableView的底部?
- 13. CoordinatorLayout底部的工具栏
- 14. 将工具栏添加到UITableView的正确方法是什么?
- 15. 如何将导航栏添加到UITableView
- 16. 如何将多个菜单部分添加到工具栏?
- 17. 页脚添加到UITableView的底部
- 18. 在顶部和底部添加剑道网格工具栏
- 19. 如何将按钮添加到extjs中的底部栏?
- 20. 棒工具栏底部
- 21. 底部工具栏布局
- 22. UITableView添加行并滚动到底部
- 23. 如何添加工具栏
- 24. ExtJS 4 - 添加底部工具栏后的额外边框
- 25. Android。如何将按钮添加到底部(导航栏)
- 26. xcode - 如何添加工具栏到UITableViewController
- 27. 如何将后退按钮添加到操作栏/工具栏
- 28. 如何将部分添加到UITableView?
- 29. 如何将工具栏添加到WPF中列表框的底部,顶部,左侧或右侧?
- 30. 如何将工具栏放在Android的底部?
什么也没有..我看到的任何帖子都是关于在顶部添加它 – snksnk