簡體   English   中英

iPhone - 如何在鍵盤上方創建滾動UIToolbar?

[英]iPhone - How to create a scrolling UIToolbar above keyboard?

你如何創建一個滾動UIToolbar ,鍵盤上方有額外的配件按鈕,它的寬度也可以滾動? 就像第一天和Byword那樣。

編輯 :我在UITextView使用

來自Byword

在此輸入圖像描述

這是我對輸入附件視圖所做的,我已經實現了條形按鈕項。 現在我很好奇我是否可以讓它水平滾動以顯示更多按鈕。

在此輸入圖像描述

在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;

這基本上在鍵盤上添加了一個工具欄,其中一個條形按鈕顯示為“完成”。

創建一個包含scrollview中所有按鈕的視圖,並將該視圖指定為inputAccessoryView或TextField。 希望你能得到我的答案。

根據您的要求創建多個條形按鈕,並將它們添加到數組中。 現在在視圖中創建一個滾動視圖。 並將scrollview作為文本字段的輸入附件視圖。

以下是一個示例: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. 將scrollview作為inputAccessoryView添加到textField

    self.textField.inputAccessoryView = scrollView;

您需要子類化UIView並添加UIToolbar作為背景視圖。 在其中添加UIScrollView,在此UIScrollView中添加Add 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

在開始引用文本時,您應該設置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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM