【iOS学习】----UIToolbar

UIToolbar也是常用的控件之一,用法如下:

    toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
    toolbar.barStyle = UIBarStyleBlackTranslucent;//半透明样式
    toolbar.tintColor = [UIColor grayColor];//背景颜色
    [self.view addSubview:toolbar];
    
    UIBarButtonItem *itemField = [[UIBarButtonItem alloc] initWithCustomView:inputField];//输入框
    
    //间隔(会把两个item之间的空间平均分布,是可变的),比如你想把三个按钮均匀放在toolbar上,就需要这个间隔隔开三个按钮
    UIBarButtonItem *itemFlexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                               target:self
                                                                               action:nil];
    //按钮
    UIBarButtonItem *itemSend = [[UIBarButtonItem alloc] initWithTitle:@"Send"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(sendBtnClicked)]; 
    //间隔是不可变的(注意与FlexibleSpace的区别)
    UIBarButtonItem *itemFixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                    target:self
                                                                                    action:nil];
    //按钮
    UIBarButtonItem *itemRedo = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRedo
                                                                              target:self
                                                                              action:@selector(sendBtnClicked)]; 
    
    NSArray *arr = [NSArray arrayWithObjects:itemField,itemFlexibleSpace,itemSend, itemFixedSpace,itemRedo,nil];
    //添加到toolbar中
    [toolbar setItems:arr];
    
    [itemField release];
    [itemFlexibleSpace release];
    [itemSend release];
    [itemFixedSpace release];
    [itemRedo release];


你可能感兴趣的:(工具栏,UIToolbar)