1

我正在尝试使用子视图显示评论列表。我将 subview 添加到 self.view,并添加了一个 tableview 以显示评论列表。现在我想让用户添加评论,我尝试在屏幕底部使用文本字段和按钮向第一个子视图添加另一个子视图,但现在我想在键盘出现时将此视图向上移动然后返回提交评论时的底部,我尝试在 textfieldBeginEditing 时更改评论视图框架,但它没有改变!我的方法行不通,你有什么想法做我想做的吗?

谢谢

这就是我正在做的,名为 myview 的视图是我想要向上移动的:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

// notification button
myView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight)];
[myView setBackgroundColor:[UIColor whiteColor]];
[myView setTag:2013];

commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];
[commentView setBackgroundColor:[UIColor redColor]];
// [commentView viewWithTag:2031];

table = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenWidth, screenHeight-100)];;
table.dataSource=self;
table.delegate=self;

UILabel *currentdate = [[UILabel alloc]  initWithFrame:CGRectMake(0,10,screenWidth-40,50)];
currentdate.backgroundColor = [UIColor clearColor];
[currentdate setTextColor: [UIColor blueColor]];
[currentdate setText:@"Comments"];
currentdate.textAlignment= UITextAlignmentCenter;
currentdate.font = [UIFont fontWithName:@"Helvetica" size:20.0];


commentFeild = [[UITextField alloc]  initWithFrame:CGRectMake(60,10,screenWidth-60,30)];

commentFeild.placeholder=@"add comment";
commentFeild.backgroundColor = [UIColor whiteColor];
[commentFeild setTextColor: [UIColor blueColor]];
commentFeild.textAlignment= UITextAlignmentRight;
commentFeild.font = [UIFont fontWithName:@"Helvetica" size:15.0];
[commentFeild.layer setCornerRadius:14.0f];
[commentFeild setTag:2113];
//[commentFeild setDelegate:self];

UIButton *doneBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
doneBtn.frame = CGRectMake(screenWidth-45, 10,40, 30);
doneBtn.backgroundColor = [ UIColor clearColor];
[doneBtn setTitle:@"done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[doneBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];

UIButton *add=[UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(5,10,50,30);
add.backgroundColor = [ UIColor clearColor];
[add setTitle:@"add" forState:UIControlStateNormal];
[add setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[add addTarget:self action:@selector(addComment) forControlEvents:UIControlEventTouchUpInside];

if(![taskObject.status isEqualToString:@"doneTask"])
{
    [commentView addSubview:commentFeild];
    [commentView addSubview:add];
}

[myView addSubview:doneBtn];
[myView addSubview:currentdate];
[myView addSubview:table];
[myView addSubview:commentView];

[self.view addSubview:myView];
4

3 回答 3

2

你需要在键盘出现和消失时使用通知

viewDidLoad方法中添加这个

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidDisappear:) name:UIKeyboardDidHideNotification object:nil];


当键盘出现和消失时,上述方法会被触发使用它来调整你的 tableview

例如

  -(void)keyboardDidShow:(NSNotification *)notification
  {
      NSLog(@"KeyBoard appeared");
      NSDictionary *info=[notification userInfo];
      NSValue *aValue=[info objectForKey:UIKeyboardFrameEndUserInfoKey];

      CGRect keyBoardRect=[aValue CGRectValue];
      keyBoardRect=[self.view convertRect:keyBoardRect fromView:nil];
      CGFloat keyBoardTop=keyBoardRect.origin.y; //i am getting the height of the keyboard

      self.aTableView.contentInset=UIEdgeInsetsMake(0, 0, keyBoardTop+50, 0); //adjust the height by setting the "contentInset"


  }


当键盘消失时这样做


  -(void)keyboardDidDisappear:(NSNotification *)notification
    {
       NSLog(@"KeyBoard Disappeared");

       [UIView beginAnimations:nil context:NULL];
       [UIView setAnimationDuration:0.2];
       self.addContactTableView.contentInset=UIEdgeInsetsMake(10, 0, 10, 0); //set to normal by setting the "contentInset" 

       [UIView commitAnimations];

    }


解决这个问题你可以满足你的要求

希望这可以帮助你:)

于 2013-10-28T07:55:56.267 回答
1

我认为你应该通过文档

于 2013-10-28T07:48:52.337 回答
0

我以为你想移动表格视图,听到的是你的代码修改了一个


       @interface ViewController (  <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
     {
        BOOL KeyboardShown;
     }

     @end

     @implementation ViewController

    - (void)viewDidLoad
    {
     [super viewDidLoad];
     KeyboardShown = NO;

// Do any additional setup after loading the view, typically from a nib.
     CGRect screenRect = [[UIScreen mainScreen] bounds];
     CGFloat screenWidth = screenRect.size.width;
     CGFloat screenHeight = screenRect.size.height;

// notification button

    UIView *myView,*commentView;

    myView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight)];
    [myView setBackgroundColor:[UIColor whiteColor]];
    [myView setTag:2013];

   commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];
   [commentView setBackgroundColor:[UIColor redColor]];
// [commentView viewWithTag:2031];

    UITableView *table;
    table = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenWidth, screenHeight-100)];;
   table.dataSource=self;
   table.delegate=self;
   table.tag = 12345;

    UILabel *currentdate = [[UILabel alloc]  initWithFrame:CGRectMake(0,10,screenWidth-40,50)];
    currentdate.backgroundColor = [UIColor clearColor];
    [currentdate setTextColor: [UIColor blueColor]];
    [currentdate setText:@"Comments"];
    currentdate.textAlignment= NSTextAlignmentCenter;
    currentdate.font = [UIFont fontWithName:@"Helvetica" size:20.0];


     UITextField *commentFeild = [[UITextField alloc] initWithFrame:CGRectMake(60,10,screenWidth-60,30)];

    commentFeild.placeholder=@"add comment";
    commentFeild.backgroundColor = [UIColor whiteColor];
    [commentFeild setTextColor: [UIColor blueColor]];
    commentFeild.textAlignment= NSTextAlignmentRight;
   commentFeild.font = [UIFont fontWithName:@"Helvetica" size:15.0];
   [commentFeild.layer setCornerRadius:14.0f];
   [commentFeild setTag:2113];
   commentFeild.delegate = self;
    //[commentFeild setDelegate:self];

   UIButton *doneBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
   doneBtn.frame = CGRectMake(screenWidth-45, 10,40, 30);
   doneBtn.backgroundColor = [ UIColor clearColor];
   [doneBtn setTitle:@"done" forState:UIControlStateNormal];
   [doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
   [doneBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];

   UIButton *add=[UIButton buttonWithType:UIButtonTypeRoundedRect];
   add.frame = CGRectMake(5,10,50,30);
   add.backgroundColor = [ UIColor clearColor];
   [add setTitle:@"add" forState:UIControlStateNormal];
   [add setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
   [add addTarget:self action:@selector(addComment)      forControlEvents:UIControlEventTouchUpInside];


     //    if(![taskObject.status isEqualToString:@"doneTask"])
     //    {
     //        [commentView addSubview:commentFeild];
     //        [commentView addSubview:add];
     //    }

[myView addSubview:doneBtn];
[myView addSubview:currentdate];
[myView addSubview:table];
[myView addSubview:commentView];
[commentView addSubview:commentFeild];
[self.view addSubview:myView];

}

  - (void)viewDidAppear:(BOOL)animated
   {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self  selector:@selector(keyboardDidDisappear:) name:UIKeyboardDidHideNotification object:nil];
  }

  - (void)viewDidDisappear:(BOOL)animated
   {
         [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
   }





 - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
   }

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
      return 1;
    }
   -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       return 5;
    }

     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
       {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

       cell.textLabel.text = @"hello";
       return cell;


   }

    - (void)keyboardDidShow:(NSNotification *)notification
     {
        //UIView *containerView = [self.view viewWithTag:2013]; comment this and get tableview by TAG
       UITableView *tableView = (UITableView *)[self.view viewWithTag:12345];
       UIView *commentView = [self.view viewWithTag:1000];//i changed the tag and see u missed the tag for this view in viewDisLoad method check it once


     if(!KeyboardShown)
     {
        KeyboardShown = YES;
         NSLog(@"KeyBoard appeared");
        NSDictionary *info=[notification userInfo];
        NSValue *aValue=[info objectForKey:UIKeyboardFrameEndUserInfoKey];

        CGRect keyBoardRect=[aValue CGRectValue];
        keyBoardRect=[self.view convertRect:keyBoardRect fromView:nil];
        CGFloat keyBoardTop=keyBoardRect.origin.y; //i am getting the height of the keyboard


        CGRect commentViewRect = commentView.frame;
         commentViewRect.origin.y = keyBoardTop - 70; //it is the height of comment view

        CGRect tableRect = tableView.frame;
        tableRect.size.height = tableRect.size.height - keyBoardTop + 77;//adjust the height



       [UIView animateWithDuration:0.2 animations:^{
       tableView.frame = tableRect;
       commentView.frame = commentViewRect;

   }];



   }


 }




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

     // UIView *containerView = [self.view viewWithTag:2013]; //comment this
     //dong same as above , ressetting the same as above
     CGRect screenRect = [[UIScreen mainScreen] bounds];
     CGFloat screenWidth = screenRect.size.width;
     CGFloat screenHeight = screenRect.size.height;

    UITableView *tableView = (UITableView *)[self.view viewWithTag:12345];
    UIView *commentView = [self.view viewWithTag:1000];

    if(KeyboardShown)
    {
       KeyboardShown = NO;
       CGRect tableRect =CGRectMake(0,50, screenWidth, screenHeight-100);
       CGRect commentViewRect = CGRectMake(0,screenHeight-70,screenWidth,70);


        [UIView animateWithDuration:0.2 animations:^{
        tableView.frame = tableRect;
        commentView.frame = commentViewRect;

      }];
    }
   }









 - (BOOL)textFieldShouldReturn:(UITextField *)textField
   {
      [textField resignFirstResponder];
      return YES;
   }

@end


于 2013-10-28T09:42:46.667 回答