How to add a UIToolbar programmatically to an iOS app?

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

提交回复
热议问题