通过拖控件,发现虽然UITextField等控件不是UIBarButtonItem类,但是也可以拖到UIToolBar上面。


仔细看,可以发现它并不是直接将UITextField加在ToolBar上面的,而是将其加在一个
UIBarButtonItem上面,然后再把这个
UIBarButtonItem加载ToolBar上面的。
那么用代码写的时候就不能通过初始化一个
UITextField,然后调用toolBar的addSubView方法来加上来。
而是要通过下面的方法加上去:
//将文本框等UIView对象加到UIToolBar的UIBarButtonItemH中
UITextField * input = [[UITextField alloc]init];
[input setFrame:CGRectMake(50, 0, 200, 31)];
[input setBorderStyle:UITextBorderStyleRoundedRect];
[input setBackgroundColor:[UIColor clearColor]];
UIBarButtonItem * textBar = [[UIBarButtonItem alloc]initWithCustomView:input];