2013年7月25日 星期四

iOS 設計出 facebook 留言的 bottom bar 隨著 text 鍵盤上升出現





step1.
首先要創一個 ViewController 

step2.
ViewController 後面加上UITextViewDelegate 如下

@interface ViewController ()<UITextViewDelegate >

{
        UITextView * commentTextView;
        UITextView * realCommentTextView;
}


step3.
ViewController 的viewDidLoad

//新增一個bottomview

 UIToolbar *bottomView = [[UIToolbar alloc] initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-49, 320, 49)];
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = [UIColor blackColor];
    bottomView.tintColor = [UIColor grayColor];
    
//新增一個輸入textview

    commentTextView = [[UITextView alloc] initWithFrame:CGRectMake(8, 10, 250, 30)];
    [bottomView addSubview:commentTextView];
    commentTextView.delegate = self;
    commentTextView.backgroundColor = [UIColor whiteColor];
    [commentTextView.layer setCornerRadius:5];
    commentTextView.clipsToBounds = YES;
    [commentTextView.layer setBorderWidth: 1.0];
    [commentTextView.layer setBorderColor: [[UIColor grayColor] CGColor]];

    commentTextView.font = [UIFont systemFontOfSize:16];

//新增一個輸入button

    UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    commentButton.frame = CGRectMake(265, 10, 45, 30);
    [commentButton setTitle:NSLocalizedString(@"Send", nil) forState:UIControlStateNormal];
    [bottomView addSubview: commentButton];
    [commentButton addTarget:self action:@selector(addCommentButPressed:) forControlEvents:UIControlEventTouchUpInside];

step4.

在新增點下後的樣子

//新增一個bottomview

UIToolbar *bottomView2 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-20-44-49, 320, 49)];
    bottomView2.backgroundColor = [UIColor blackColor];
    bottomView2.tintColor = [UIColor grayColor];


//新增一個輸入textview   

    realCommentTextView = [[UITextView alloc] initWithFrame:CGRectMake(8, 10, 250, 30)];
    realCommentTextView.delegate = self;
    realCommentTextView.backgroundColor = [UIColor whiteColor];
    realCommentTextView.clipsToBounds = YES;
    [realCommentTextView.layer setCornerRadius:5];
    [bottomView2 addSubview:realCommentTextView];
    [realCommentTextView.layer setBorderWidth: 1.0];
    [realCommentTextView.layer setBorderColor: [[UIColor grayColor] CGColor]];
    realCommentTextView.font = [UIFont systemFontOfSize:16];
    
//新增一個輸入button

    UIButton *commentButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    commentButton2.frame = CGRectMake(265104530);
    [commentButton2 setTitle:NSLocalizedString(@"Send"nilforState:UIControlStateNormal];
    [bottomView2 addSubview: commentButton2];
    [commentButton2 addTarget:self action:@selector(addCommentButPressed:) forControlEvents:UIControlEventTouchUpInside];



step5.

讓點下後出現bottomView2

    commentTextView.inputAccessoryView = bottomView2;

step6.

加上UITextViewDelegate當輸入很多的時候高會改變並且開起scrolls

#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView
{
    if(textView.contentSize.height > 50)
    {
        textView.superview.frame = CGRectMake(0, 0, 320, 98);
        textView.frame = CGRectMake(5, 14.5, 250, 60);
        [textView scrollsToTop];
    }
}


0 意見:

張貼留言