0

I'm trying to shrink the height of my UITableView when the keyboard pops up.

I started by using an UITableViewController, but I red that it's not able to manage the size of it. Then I switched to an UIViewController, but when I set the frame of my UITableView it won't change in size. When I use my storyboard to resize it manually the size changes, but when using code it won't work. Can anyone help me out?

My .h file:

#import <UIKit/UIKit.h>
#import "PHFComposeBarView.h"

@interface DetailView : UIViewController <UITableViewDelegate, UITableViewDataSource, PHFComposeBarViewDelegate>
{
    CGRect kInitialViewFrame;
    int cellAmount;
}

@property (readonly, nonatomic) PHFComposeBarView *composeBarView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

My .m file:

#import "DetailView.h"

@implementation DetailView

@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tableView.delegate = (id)self;
    self.tableView.dataSource = (id)self;

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

    kInitialViewFrame = CGRectMake(0, 0, screenWidth, screenHeight);

    cellAmount = 30;


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillToggle:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillToggle:)
                                                 name:UIKeyboardWillHideNotification
                                               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 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CommentCell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

@synthesize composeBarView = _composeBarView;
- (PHFComposeBarView *)composeBarView {
    if (!_composeBarView) {
        CGRect frame = CGRectMake(0.0f,
                                  kInitialViewFrame.size.height - PHFComposeBarViewInitialHeight,
                                  kInitialViewFrame.size.width,
                                  PHFComposeBarViewInitialHeight);
        _composeBarView = [[PHFComposeBarView alloc] initWithFrame:frame];
        [_composeBarView setMaxLinesCount:5];
        [_composeBarView setPlaceholder:@"Laat een bericht achter..."];
        [_composeBarView setDelegate:(id)self];
    }

    return _composeBarView;
}

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

    NSDictionary* userInfo = [notification userInfo];
    NSTimeInterval duration;
    UIViewAnimationCurve animationCurve;
    CGRect startFrame;
    CGRect endFrame;
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&duration];
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]    getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]        getValue:&startFrame];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]          getValue:&endFrame];

    NSInteger signCorrection = 1;
    if (startFrame.origin.y < 0 || startFrame.origin.x < 0 || endFrame.origin.y < 0 || endFrame.origin.x < 0)
        signCorrection = -1;

    CGFloat widthChange  = (endFrame.origin.x - startFrame.origin.x) * signCorrection;
    CGFloat heightChange = (endFrame.origin.y - startFrame.origin.y) * signCorrection;

    CGFloat sizeChange = UIInterfaceOrientationIsLandscape([self interfaceOrientation]) ? widthChange : heightChange;

    CGRect newContainerFrame = [[self composeBarView] frame];
    newContainerFrame.origin.y += sizeChange;

    [UIView animateWithDuration:duration
                          delay:0
                        options:(animationCurve << 16)|UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [[self composeBarView] setFrame:newContainerFrame];
                     }
                     completion:NULL];

    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.frame.size.height - (sizeChange + self.composeBarView.frame.size.height));

}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.view addSubview:[self composeBarView]];

    self.tableView.frame = CGRectMake(0, 0, 200, 250);
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.view endEditing:YES];
    [[self composeBarView] removeFromSuperview];
}


@end
Jasper Fioole
  • 449
  • 1
  • 5
  • 25
  • Try to NSLog the frames and value or debug the values, I think they are not getting proper values specially `sizeChange` , `endFrame` and `startFrame` – iphonic Nov 16 '13 at 20:08
  • @iphonic my composebar moves the right amount of pixels so these must be filled. But if you check my viewWillAppear method you will see that I try setting values manually but it still doesn't work. `self.tableView.frame = CGRectMake(0, 0, 200, 250);` – Jasper Fioole Nov 16 '13 at 20:14
  • Are you using Autolayouts in storyboard? – frankWhite Nov 16 '13 at 22:06
  • @frankWhite Yes I am, I tried disabling it but nothing changes. – Jasper Fioole Nov 16 '13 at 22:16
  • if you use xcode5, I think you can try 2 ways: 1) programmatically change constraints (i don't use this method) 2) you can add empty VC in storyboard and entirely create tables view via code. (this is not good because need hardcode some position for height etc.) Hope it helps – frankWhite Nov 16 '13 at 23:02
  • I will try those methods. I like using the interface builder to create custom cells. Is it possible to use custom cells when using method 2? – Jasper Fioole Nov 17 '13 at 10:08
  • I think yes. You can create separate xibs for some UI elements. (As example http://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files) – frankWhite Nov 17 '13 at 12:50
  • I wil try some things, I'm not used to work with XIB files, I started developing when storyboards came out. – Jasper Fioole Nov 17 '13 at 12:56
  • @frankWhite I think I got it to work now, I readded the tableview and it works now thx. – Jasper Fioole Nov 17 '13 at 14:16

1 Answers1

1

I have made a simple project for you to change the height of tableView... please refer the link

https://www.dropbox.com/sh/jvgz5289idso0e5/3hfplMANM5

Ashish
  • 1,899
  • 20
  • 22