ios 弹出键盘上面带UITextView 的评论界面

.h里面写的两个属性。

//评论的时候弹出来的。

@property (retain, nonatomic) UIView *mainView;

@property (retain, nonatomic) UITextView *myTextV1;

 

ViewDidLoad 里面写的。

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(changeContentViewPoint:) name:UIKeyboardWillShowNotificationobject:nil];

    self.myTextV1 = [[UITextViewalloc]initWithFrame:CGRectMake(0, 0, 280, 40)];

    self.myTextV1.layer.cornerRadius = 5;

    self.myTextV1.layer.masksToBounds = YES;

    

    UIButton *fasongBut = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    fasongBut.frame = CGRectMake(280, 0, 40, 40);

    [fasongBut setTitle:@"确定"forState:UIControlStateNormal];

    [fasongBut addTarget:selfaction:@selector(pinglunQueding:) forControlEvents:UIControlEventTouchUpInside];

    

    

    self.mainView = [[UIViewalloc]initWithFrame:CGRectMake(0, kScreenHeight, 320, 40)];

    [self.view addSubview:self.mainView];

    [self.mainViewaddSubview:self.myTextV1];

 

    [self.mainView addSubview:fasongBut];

 

 

 

下面是操作的三个方法。

-(void)pinglunAction:(UIButton *)sender{

    self.mainView.hidden = NO;

    [self.viewbringSubviewToFront:self.mainView];

    [self.myTextV1becomeFirstResponder];

    

}

-(void)pinglunQueding:(UIButton *)sender{

    [self.myTextV1resignFirstResponder];

    self.mainView.hidden = YES;

    self.mainView.center = CGPointMake(self.mainView.center.x, kScreenHeight);

}

 

// 根据键盘状态,调整_mainView的位置

- (void) changeContentViewPoint:(NSNotification *)notification{

    

    NSDictionary *userInfo = [notification userInfo];

    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGFloat keyBoardEndY = value.CGRectValue.origin.y// 得到键盘弹出后的键盘视图所在y坐标

    

    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    

    // 添加移动动画,使视图跟随键盘移动

    [UIViewanimateWithDuration:duration.doubleValueanimations:^{

        [UIViewsetAnimationBeginsFromCurrentState:YES];

        [UIViewsetAnimationCurve:[curve intValue]];

        

        self.mainView.center = CGPointMake(self.mainView.center.x, keyBoardEndY - kStateBarHeight - self.mainView.bounds.size.height/2.0);   // keyBoardEndY的坐标包括了状态栏的高度,要减去

        

    }];

    

    

    

 

}

 

猜你喜欢

转载自zhangmingwei.iteye.com/blog/1968109