【问题标题】:iPhone - How to create a scrolling UIToolbar above keyboard?iPhone - 如何在键盘上方创建滚动 UIToolbar?
【发布时间】:2013-08-18 09:33:57
【问题描述】:

你如何创建一个滚动的UIToolbar,它在键盘上方有额外的附件按钮,也可以在其宽度上滚动?就像第一天和 Byword 所做的那样。

编辑:我在UITextView中使用

来自词源

这是我对输入附件视图所做的,我已经实现了条形按钮项目。现在我想知道是否可以让它水平滚动以显示更多按钮。

【问题讨论】:

  • 您使用的是 UITextField 还是 UITextView?
  • 我当然在使用 UITextView

标签: iphone ios objective-c keyboard uitoolbar


【解决方案1】:

在 viewDidLoad 中使用此代码:

UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done:)],nil];
[numberToolbar sizeToFit];
self.yourTextView.inputAccessoryView = numberToolbar;

这基本上在键盘上添加了一个工具栏,带有一个显示“完成”的栏按钮。

【讨论】:

    【解决方案2】:

    创建一个视图,其中包含滚动视图中的所有按钮,并将该视图分配为 inputAccessoryView 或您的 TextField。希望你能得到我的答复。

    【讨论】:

      【解决方案3】:

      根据您的要求创建多个条形按钮并将它们添加到数组中。 现在在视图中创建一个滚动视图。并将滚动视图作为文本字段的输入附件视图。

      这是一个例子: 1.根据您的要求创建条形按钮

      UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
          UIBarButtonItem* button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(done:)];
          UIBarButtonItem* button3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(done:)];
          UIBarButtonItem* button4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(done:)];
          UIBarButtonItem* button5 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRedo target:self action:@selector(done:)];
      
      1. 将它们添加到数组中

         NSArray *itemsArray = [NSArray arrayWithObjects:button1,button2,button3,button4,button5,nil];
        
      2. 在 UIView 中创建滚动视图

        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.frame = toolbar.frame;
        scrollView.bounds = toolbar.bounds;
        scrollView.autoresizingMask = toolbar.autoresizingMask;
        scrollView.showsVerticalScrollIndicator = true;
        scrollView.showsHorizontalScrollIndicator = true;
        scrollView.scrollEnabled = YES;
        
        //scrollView.bounces = false;
        UIView *superView = toolbar.superview;
        [toolbar removeFromSuperview];
        toolbar.autoresizingMask = UIViewAutoresizingNone;
        toolbar.frame = CGRectMake(0, 0, 400, toolbar.frame.size.height);
        toolbar.bounds = toolbar.frame;
        [toolbar setItems:itemsArray];
        scrollView.contentSize = toolbar.frame.size;
        [scrollView addSubview:toolbar];
        [superView addSubview:scrollView];
        
      3. 将滚动视图作为inputAccessoryView 添加到textField

        self.textField.inputAccessoryView = scrollView;

      【讨论】:

        【解决方案4】:

        您需要继承 UIView 并添加一个 UIToolbar 作为背景视图。 在其中添加 UIScrollView 在此 UIScrollView 中添加添加 UIButtons。例如:

        @implementation CustomAccessoryView
        - (id)initWithFrame:(CGRect)frame
        {
            self = [super initWithFrame:frame];
            if (self) {
                self.backgroundColor = [UIColor clearColor];
                // Make toolbar as faked background
                UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.frame];
                toolbar.barStyle = UIBarStyleBlackTranslucent;
                [toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        
                [self addSubview:toolbar];
                [self sendSubviewToBack:toolbar]; 
        
                //Insert UIScrollVIew as subview and add button inside it
        
            }
            return self;
        }
        
        @end
        

        【讨论】:

          【解决方案5】:

          在开始编辑文本时,您应该设置 uiview 动画。

          [UIView beginAnimations:nil context:nil];
          CGRect rect=self.view.frame;
          rect.origin.y=-50;
          self.view.frame=rect;
          [UIView commitAnimations];
          

          结束编辑之后

          [UIView beginAnimations:nil context:nil];
          CGRect rect=self.view.frame;
          rect.origin.y=0;
          self.view.frame=rect;
          [UIView commitAnimations];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-03-04
            • 2014-03-20
            • 2013-02-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-01-05
            • 2018-04-30
            相关资源
            最近更新 更多