如何以编程方式将UIToolbar添加到iOS应用程序?
似乎无法find问题标题所描述的教程。 我想了解在哪里UIToolbar需要声明,以及如何把它放到我的视图层。
UIToolbar
是UIView
的子类,所以你的问题的简短答案是:就像任何其他视图。
具体而言,这是如何以编程方式创build工具栏的示例。 这段代码中的上下文是视图控制器的viewDidLoad
。
UIToolbar *toolbar = [[UIToolbar alloc] init]; toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44); NSMutableArray *items = [[NSMutableArray alloc] init]; [items addObject:[[[UIBarButtonItem alloc] initWith....] autorelease]]; [toolbar setItems:items animated:NO]; [items release]; [self.view addSubview:toolbar]; [toolbar release];
有关详细信息,请参阅UIToolbar和UIBarButtonItem文档。
如果您使用的是UINavigationController
那么默认情况下工具栏会自带。
您可以使用以下代码行添加它:
self.navigationController.toolbarHidden = NO;
要将button添加到工具栏,您可以使用以下代码:
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
flexibleItem
用于保持上面创build的两个button之间的适当距离。
现在,您可以添加这三个项目,以使它们在您的视图中可见。
NSArray *items = [NSArray arrayWithObjects:item1, flexibleItem, item2, nil]; self.toolbarItems = items;
我希望这个对你有用。
要显示工具栏的底部,两个button之间的距离在左侧,另一个在右侧
-(void)showToolBar { CGRect frame, remain; CGRectDivide(self.view.bounds, &frame, &remain, 44, CGRectMaxYEdge); UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame]; UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"Send" style:UIBarButtonItemStyleDone target:self action:nil]; UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem *button2=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:nil]; [toolbar setItems:[[NSArray alloc] initWithObjects:button1,spacer,button2,nil]]; [toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; [self.view addSubview:toolbar]; }
注意:要给button之间的空间,我们添加行如下
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
并添加spacer到
[toolbar setItems:[[NSArray alloc] initWithObjects:button1,spacer,button2,nil]];
试试这个简单的方法:
UIToolbar *toolbar = [[UIToolbar alloc] init]; toolbar.frame = CGRectMake(0, 0, 300, 44); UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"Send" style:UIBarButtonItemStyleDone target:self action:@selector(sendAction)]; UIBarButtonItem *button2=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(cancelAction)]; [toolbar setItems:[[NSArray alloc] initWithObjects:button1,button2, nil]]; [self.view addSubview:toolbar];
这是你如何在你的应用程序中实现UIToolbar
。
// declare frame of uitoolbar UIToolBar *lotoolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 170, 320, 30)]; [lotoolbar setTintColor:[UIColor blackColor]]; UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"DATE" style:UIBarButtonItemStyleDone target:self action:@selector(dateToolbardoneButtonAction)]; UIBarButtonItem *button2=[[UIBarButtonItem alloc]initWithTitle:@"TIME" style:UIBarButtonItemStyleDone target:self action:@selector(timeToolbarbuttonAction)]; [lotoolbar setItems:[[NSArray alloc] initWithObjects:button1, nil]; [lotoolbar setItems:[[NSArray alloc] initWithObjects:button2, nil]; [mainUIview addSubview:lotoolbar];
你也应该实现下面的委托方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { } - (void)textViewDidChange:(UITextView *)textView{ NSLog(@"textViewDidChange:"); } - (void)textViewDidChangeSelection:(UITextView *)textView{ NSLog(@"textViewDidChangeSelection:"); } - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { [lotextview setText:@""]; NSLog(@"textViewShouldBeginEditing:"); return YES; }
iOS 11 + SWIFT 4 + Xcode 9 +约束
适用于两者 +
override func viewDidLoad() { super.viewDidLoad() print(UIApplication.shared.statusBarFrame.height)//44 for iPhone x, 20 for other iPhones navigationController?.navigationBar.barTintColor = .red let toolBar = UIToolbar() var items = [UIBarButtonItem]() items.append( UIBarButtonItem(barButtonSystemItem: .save, target: nil, action: nil) ) items.append( UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(tapsOnAdd)) ) toolBar.setItems(items, animated: true) toolBar.tintColor = .red view.addSubview(toolBar) toolBar.translatesAutoresizingMaskIntoConstraints = false if #available(iOS 11.0, *) { let guide = self.view.safeAreaLayoutGuide toolBar.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true toolBar.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true toolBar.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true toolBar.heightAnchor.constraint(equalToConstant: 44).isActive = true } else { NSLayoutConstraint(item: toolBar, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: toolBar, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: toolBar, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true toolBar.heightAnchor.constraint(equalToConstant: 44).isActive = true } }