2013-11-28 146 views
0

我一直試圖在我的鍵盤上方加入UIView/Toolbar,但沒有運氣。當我添加了一個工具欄時,它被加擾,因此我需要將它放入UIView,但UIView不希望出現在鍵盤上方。下面的代碼:inputaccessoryview not showing(StoryBoard)

我的頭:

@property (nonatomic, Strong) IBOutlet UITextView *textView; 
@property (nonatomic, strong) IBOutlet UIToolbar *TitleBar; 
@property (nonatomic, weak) IBOutlet UIView *AddView; 

ViewDidLoad

- (void)viewDidLoad 
{  
    // observe keyboard hide and show notifications to resize the text view appropriately 
    /*[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification 
               object:nil]; 
    */ 

    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { 
     // iOS 7 
     [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]; 
    } else { 
     // iOS 6 
     [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 
    } 

    self.attributionTitle.delegate = self; 
    self.attribution.delegate = self; 
    textView.scrollEnabled = YES; 
    // quoteText.layer.borderColor = [UIColor blackColor].CGColor; 
    // quoteText.layer.borderWidth = 1.0f; 

    // textView.delegate = self; // code or in IB 

    [textView becomeFirstResponder]; 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

textViewDidBeginEditing

-(void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    self.textView.inputAccessoryView = self.AddView; 
} 

這裏是展示UIView連接:​​

回答

1

我將textView.inputAccessoryView = AddView;添加到ViewDidLoad,然後從故事板中刪除視圖並重新制作它。最後,我將UIView添加到底部的黑色欄中。

+0

這是什麼意思,你把視圖添加到底部的黑色欄?是否有可能在故事板中定義一個實際上未放置在擁有它的視圖控制器中的視圖?這樣的視圖在故事板中直觀地出現在哪裏,並且通過某些segue鏈接到父視圖控制器? – devios1

0

textViewDidBeginEditing中添加inputAccessoryView可能太晚了。輸入附件視圖應在此之前設置,例如,在viewDidLoad方法中。

嘗試類似:

-(void)viewDidLoad{ 
    [super viewDidLoad]; 

UIView 

    myTextField.inputAccessoryView = [self accessoryViewWithPreviousEnabled:NO nextEnabled:YES]; 

    // more stuff as required... 
} 

以及創建一個/下一個按鈕的方法(你需要的按鈕提供你自己的圖像,並實現了previousAccessoryViewButtonTapped:previousAccessoryViewButtonTapped:方法)。需要兩個參數BOOL來指示是否應啓用上一個和/或下一個按鈕。

#pragma mark - Accessory view methods 

-(UIView *)accessoryViewWithPreviousEnabled:(BOOL)previousEnabled nextEnabled:(BOOL)nextEnabled{ 
    previousButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    previousButton.frame = CGRectMake(10, 2, 60, 30); 
    [previousButton setImage:[UIImage imageNamed:PREVIOUS_BUTTON] forState:UIControlStateNormal]; 
    previousButton.enabled = previousEnabled; 
    [previousButton addTarget:self action:@selector(previousAccessoryViewButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

    nextButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    nextButton.frame = CGRectMake(80, 2, 60, 30); 
    [nextButton setImage:[UIImage imageNamed:NEXT_BUTTON] forState:UIControlStateNormal]; 
    nextButton.enabled = nextEnabled; 
    [nextButton addTarget:self action:@selector(nextAccessoryViewButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

    UIView *transparentBlackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 34)]; 
    transparentBlackView.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.6f]; 

    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 34)]; 
    [accessoryView addSubview:transparentBlackView]; 
    [accessoryView addSubview:previousButton]; 
    [accessoryView addSubview:nextButton]; 

    return accessoryView; 
} 

請注意,這種方法是針對橫向iPad的硬編碼。您需要爲iPhone更改它。

+0

是否可以在故​​事板中設置它? – user2977529

0

問題是您的self.AddView已經在您的界面中(因爲您將它放在故事板中)。它不能一次在兩個地方。

+0

那麼我該如何解決這個問題?我必須硬編碼嗎? @matt – user2977529