如何为 UIToolbar 上的按钮添加滚动?

Posted

技术标签:

【中文标题】如何为 UIToolbar 上的按钮添加滚动?【英文标题】:How to add scrolling for buttons on UIToolbar? 【发布时间】:2012-10-31 09:52:41 【问题描述】:

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

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

非常感谢您的帮助!

【问题讨论】:

【参考方案1】:

对于斯威夫特

假设我想将 7 个UIBarButtonItem 添加到我的UIToolBar

先创建一个scrollView,然后添加toolBar作为子视图

// In viewDidLoad

let scrollView = UIScrollView(frame: CGRect(x: 0, y: view.frame.height-44, width: view.frame.width, height: 50))

    let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 1000, height: scrollView.frame.height))

    let btn1 = UIBarButtonItem()
    let btn2 = UIBarButtonItem()
    let btn3 = UIBarButtonItem()
    let btn4 = UIBarButtonItem()
    let btn5 = UIBarButtonItem()
    let btn6 = UIBarButtonItem()
    let btn7 = UIBarButtonItem()
    toolBar.items = [btn1, btn2, btn3, btn4, btn5, btn6, btn7]

scrollView.addSubview(toolBar)

// The below line is important for scrollView to work
scrollView.contentSize = CGSize(width: 1000, height: 50)

最后添加 scrollView 作为你的 textField inputAccessoryView

textField.inputAccessoryView = scrollView

希望对你有帮助:]

【讨论】:

【参考方案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];

【讨论】:

你试过这个代码吗?!我无法在 UIBarButtonItem 上设置操作。 很遗憾,无法正确计算 X。【参考方案3】:

快速版本

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 x 12 但它们不适合,我认为自动添加了一些边距,我没有详细说明如何计算按钮的大小,所以我只是尝试直到找到数字对我来说效果很好。

【讨论】:

我尝试了你的代码,但一切正常,但是当我滚动工具栏时它没有滚动【参考方案4】:

创建一个 UIScrollView,根据你的要求设置它的 contentSize 将它添加为 UIToolbar 上的子视图。在 UIScrollView 上添加尽可能多的按钮并享受滚动...

【讨论】:

如何在UIScrollView上添加UIBarButtonItem

以上是关于如何为 UIToolbar 上的按钮添加滚动?的主要内容,如果未能解决你的问题,请参考以下文章

如何为栏按钮添加徽章?

如何为颤动按钮添加边框?

在对话窗口中使用 Glade,如何为按钮添加响应

如何为 python kivy 绘画应用程序添加清除按钮

vc++如何为窗口添加滚动条?

macOS 上的 SwiftUI:如何为 onDelete 启用 UI(从列表中删除)