etab
etab

Reputation: 199

move subview when keyboard show up

i am trying to show comments list by using subview. I added subview to self.view, and i added a tableview to it in order to show comments list. now i want to enable user to add comment, i tried to add another subview to the first one with text field and button in the bottom of the screen, but now i want to move this view up when the keyboard show up and then back to bottom when submitting the comment, i tried to change commentView frame when textfieldBeginEditing but it do not change !! my way do not work, do you have any idea to do what i want ?

thanks

this is what i am doing, the view called myview is what i want to move up:

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];

Upvotes: 1

Views: 914

Answers (3)

Shankar BS
Shankar BS

Reputation: 8402

i thought u want to move the tableview, hear is the code of yours modified one


       @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


Upvotes: 0

Shankar BS
Shankar BS

Reputation: 8402

U need to use notifications when keyboard appeared and disappeared

in viewDidLoad method add this

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


above methods get fired when keyboard appeared and disappeared use this to make adjustments of ur tableview

for exaple

  -(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"


  }


when keyboard disappeared do like this


  -(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];

    }


a little work around this u may achieve your requirement

hope this helps u :)

Upvotes: 2

Krishna Kumar
Krishna Kumar

Reputation: 1652

I think you should go through the doc

Upvotes: 1

Related Questions