在键盘上更改UITableView大小

时间:2013-11-16 20:00:00

标签: ios uitableview storyboard size frame

当键盘弹出时,我正试图缩小UITableView的高度。

我开始使用UITableViewController,但我认为它无法管理它的大小。然后我切换到UIViewController,但是当我设置UITableView的框架时,它的大小不会改变。当我使用我的故事板手动调整大小时,大小会发生变化,但在使用代码时,它将无法正常工作。任何人都可以帮助我吗?

我的.h文件:

#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

我的.m文件:

#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

1 个答案:

答案 0 :(得分:1)

我已经为你做了一个简单的项目来改变tableView的高度...请参考链接

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