2012-10-31 44 views
0

如何添加UIToolbar上的UIBarButtonItem按钮的滚动(以在工具栏上放置许多按钮)?如何在UIToolbar上添加滚动按钮?

buttonDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonDoneDown)]; 
NSArray *itemsArray = [NSArray arrayWithObjects:buttonDone, nil]; 
[toolbar setItems:itemsArray]; 

非常感谢您的帮助!

回答

2

更换工具栏的SuperView:

buttonDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonDoneDown)]; 
NSArray *itemsArray = [NSArray arrayWithObjects:buttonDone, nil]; 

UIScrollView *scrollView = [[UIScrollView alloc] init]; 
scrollView.frame = toolbar.frame; 
scrollView.bounds = toolbar.bounds; 
scrollView.autoresizingMask = toolbar.autoresizingMask; 
scrollView.showsVerticalScrollIndicator = false; 
scrollView.showsHorizontalScrollIndicator = false; 
//scrollView.bounces = false; 
UIView *superView = toolbar.superview; 
[toolbar removeFromSuperview]; 
toolbar.autoresizingMask = UIViewAutoresizingNone; 
toolbar.frame = CGRectMake(0, 0, X, toolbar.frame.size.height); 
toolbar.bounds = toolbar.frame; 
[toolbar setItems:itemsArray]; 
scrollView.contentSize = toolbar.frame.size; 
[scrollView addSubview:toolbar]; 
[superView addSubview:scrollView]; 
+0

你有没有试过这段代码?!我无法在UIBarButtonItem上设置操作。 –

+0

什么是CGRectMake中的X(0,0,X,toolbar.frame.size.height)??? – Nublodeveloper

+0

不幸的是,没有办法正确计算X. – Dmitry

-1

创建一个UIScrollView,根据你的需求设置它的contentSize将它作为子视图添加到UIToolbar。加上UIScrollView的许多按钮和滚动享受...

+2

如何在'UIScrollView'上添加'UIBarButtonItem'? – Dmitry

0

迅速版本

func addToolBar(textField: UITextView){ 
    let toolBar = UIToolbar() 
    toolBar.barStyle = UIBarStyle.default 
    toolBar.isTranslucent = true 
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) 
    let bold = UIBarButtonItem(image: #imageLiteral(resourceName: "download 12.01.19.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(boldFunc)) 
    let italic = UIBarButtonItem(image: #imageLiteral(resourceName: "italic-png-image-54776.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(italicFunc)) 
    let underlined = UIBarButtonItem(image: #imageLiteral(resourceName: "underlined_font-512.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(underlineFunc)) 
    let strikeThrough = UIBarButtonItem(image: #imageLiteral(resourceName: "Strikethrough_font_awesome.svg.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(strikeFunc)) 
    let size = UIBarButtonItem(title: "\(fontSize)", style: UIBarButtonItemStyle.done, target: self, action: #selector(changeSize)) 
    let textColor = UIBarButtonItem(image: #imageLiteral(resourceName: "text_color1600.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(changetextColor)) 
    let backgroundColor = UIBarButtonItem(image: #imageLiteral(resourceName: "background color.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(changebackgroundColor)) 
    let textLeft = UIBarButtonItem(image: #imageLiteral(resourceName: "left-27877_960_720.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(alignLeft)) 
    let textRight = UIBarButtonItem(image: #imageLiteral(resourceName: "align_right_filled1600.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(alignRight)) 
    let textCenter = UIBarButtonItem(image: #imageLiteral(resourceName: "center.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(alignCenter)) 
    let pic = UIBarButtonItem(image: #imageLiteral(resourceName: "add.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(appendPic)) 
    let bulletpoint = UIBarButtonItem(image: #imageLiteral(resourceName: "bulletpoint.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(makeBulletpoints)) 
    toolBar.setItems([bold, italic, underlined, strikeThrough, size, textColor, backgroundColor, textLeft, textRight, textCenter, pic, bulletpoint], animated: false) 
    toolBar.isUserInteractionEnabled = true 
    toolBar.sizeToFit() 
    toolBar.frame = CGRect(x: 0, y: 0, width: 33 * 12, height: toolBar.frame.size.height) 
    textField.delegate = self 
    //////////try to add a scroll view 
    let scrollView = UIScrollView() 
    scrollView.frame = toolBar.frame; 
    scrollView.bounds = toolBar.bounds; 
    scrollView.autoresizingMask = toolBar.autoresizingMask; 
    scrollView.showsVerticalScrollIndicator = false; 
    scrollView.showsHorizontalScrollIndicator = false; 
    scrollView.contentSize = toolBar.frame.size; 
    scrollView.addSubview(toolBar) 
    textField.inputAccessoryView = scrollView 
} 
当你计算宽度

toolBar.frame = CGRect(x: 0, y: 0, width: 33 * 12, height: toolBar.frame.size.height) 

宽度将取决于您使用的按钮的大小。我有12个按钮,按钮上的所有图像都是25 x 25,并且有一个文本按钮。首先我写了25×12,但他们不适合,我认为有自动添加一些边距,我没有去详细了解如何计算按钮的大小,所以我只是试验,直到我找到了数字为我工作得很好。

相关问题