How to add a UIToolbar programmatically to an iOS app?

后端 未结 7 1113
太阳男子
太阳男子 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: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;
    }
    

提交回复
热议问题