0
4

4 回答 4

1

是的,它可以做到 - 只是尝试过(不是很好的 UI 设计,但也许它适合你的要求。)。您必须设置滚动视图的 contentSize。

将 UIScrollView 添加到您的故事板。在其中添加一个 UIToolBar 并使其尽可能宽(可能比 UIScrollView 更宽)。添加您需要的任何工具栏项目。

为 scrollView 和工具栏(以及您的工具栏项目)制作 IBOutlets。然后在viewDidLoad设置scrollView.contentSize

myScrollView.contentSize = CGSizeMake(myToolBar.frame.size.width, myToolBar.frame.size.height);

更新

试试这段代码,看看你的元素的真正尺寸是多少

- (void)viewDidLoad
{
    [super viewDidLoad];

    myScrollView.contentSize = CGSizeMake(myToolBar.frame.size.width, myToolBar.frame.size.height);

    CGRect myScrollViewRect = myScrollView.frame;
    NSLog(@" myScrollView %f  %f  %f  %f", myScrollViewRect.origin.x,
          myScrollViewRect.origin.y,
          myScrollViewRect.size.width,
          myScrollViewRect.size.height);

    CGRect toolbarRect = myToolBar.frame;
    NSLog(@" toolbarRect %f  %f  %f  %f", toolbarRect.origin.x,
          toolbarRect.origin.y,
          toolbarRect.size.width,
          toolbarRect.size.height);
}

我得到下面的输出。注意工具栏比scrollView 宽吗?如果你的不是更宽,它就不会滚动(怎么可能 - 没有地方可以滚动到)。

2012-07-10 14:29:34.494垃圾[4275:707] MyScrollView 0.000000 605.000000 778.000000 128.000000 2012-07-10 2012-07-10 14:29:29:34.498

于 2012-07-10T01:04:49.900 回答
0

希望这可以帮助一些人。我在这里找到了答案: 如何为 UIToolbar 上的按钮添加滚动?

基本上,您需要从其父视图中删除工具栏,重新设置工具栏的一些属性,将其设置为滚动视图的子视图,然后将滚动视图设置为您最初删除的超级视图的子视图。这一切都在那个链接中。非常适合我。

于 2013-01-04T07:54:30.423 回答
0

只好自己做。你不能。您可以做的最好的事情是放置一个工具栏并在其顶部添加一个 UIScrollView。希望这可以帮助。干杯!

于 2012-07-10T00:16:10.503 回答
0

迅捷4版本

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

于 2017-11-29T11:06:34.263 回答