How to add a UIToolbar programmatically to an iOS app?

后端 未结 7 1080
太阳男子
太阳男子 2020-12-04 10:01

Can\'t seem to find a tutorial which does as the question title describes. I\'d like to understand just where the UIToolbar needs to be declared and how to get it onto my vi

相关标签:
7条回答
  • 2020-12-04 10:35

    Try this simple Method:

        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];
    
    0 讨论(0)
  • 2020-12-04 10:42

    iOS 11+ SWIFT 4 + Xcode 9 + Constraints

    Works for both landscape + Portrait

        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
            }
    
        }
    
    0 讨论(0)
  • 2020-12-04 10:42

    To show the Toolbar at the bottom with space between two button on at Left Side , and another at Right side

    -(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];
    }
    

    Note: To Give space between to Button we add line as below

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    

    and add spacer to the

    [toolbar setItems:[[NSArray alloc] initWithObjects:button1,spacer,button2,nil]];
    
    0 讨论(0)
  • 2020-12-04 10:47

    Swift 5:

    Result:

    Code:

    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.view.backgroundColor = .systemBackground
        
        self.navigationController?.isToolbarHidden = false
        
        let toolBarItems = ["Tab1","Tab2"]
        segmentedControl = UISegmentedControl(items: toolBarItems)
        segmentedControl.selectedSegmentIndex = 0
    
        let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        let cameraBarButtonItem = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: nil)
        let segmentedControlBarButtonItem = UIBarButtonItem(customView: segmentedControl)
        let addBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addAction))
        self.toolbarItems = [cameraBarButtonItem, space, segmentedControlBarButtonItem, space, addBarButtonItem]
        
    }
    
    @objc func addAction() {
        print("Add")
    }
    
    0 讨论(0)
  • 2020-12-04 10:48

    UIToolbar is a subclass of UIView, so the short answer to your question is: just like any other view.

    Specifically, this is an example of how to programmatically create a toolbar. The context in this snippet is viewDidLoad of a view controller.

    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];
    

    See UIToolbar and UIBarButtonItem documentation for details.

    0 讨论(0)
  • 2020-12-04 10:51

    This is how you implement a UIToolbar in your app.

    // 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];
    

    You should also have to implement the following delegate methods:

    - (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;
    }
    
    0 讨论(0)
提交回复
热议问题