inputaccessoryview未显示(StoryBoard)

时间:2013-11-28 18:39:37

标签: ios iphone xcode uiview xcode5

我一直在尝试在键盘上方加UIView / Toolbar,但没有运气。当我添加工具栏时,它被加扰,因此我需要将其放入UIView,但UIView不希望出现在键盘上方。代码如下:

我的标题:

 @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已连接:enter image description here

3 个答案:

答案 0 :(得分:1)

我将textView.inputAccessoryView = AddView;添加到ViewDidLoad,然后从我的故事板中删除了该视图并重新制作。最后,我将UIView添加到底部黑条。

答案 1 :(得分:0)

inputAccessoryView中添加textViewDidBeginEditing可能为时已晚。输入附件视图应在此之前设置,例如,在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更改它。

答案 2 :(得分:0)

问题是您的self.AddView已经在您的界面中(因为您将它放在故事板中)。它不能同时在两个地方。