2012-10-31 69 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,但他們不適合,我認爲有自動添加一些邊距,我沒有去詳細瞭解如何計算按鈕的大小,所以我只是試驗,直到我找到了數字爲我工作得很好。

相關問題