0

topView、tableView、textViewの3つのビューを持つviewControllerがあります。

私は彼らにこのようにレイアウトしてもらいたい:

ポートレートモードで-

|---------------|  
|    topView    |  
|---------------|
|               |
|               |
|   tableView   |
|               |
|               |
|---------------|               
|   textView    |
|---------------|

ランドスケープモードの場合:

|--------------------------|  
|         topView          |  
|--------------------------|
|                          |
|        tableView         |
|--------------------------|               
|        textView          |
|--------------------------|

そして、キーボードが上がっているとき:

|---------------|  
|               |
|   tableView   |
|               |
|---------------|               
|   textView    |
|---------------|  
|               |
|   keyboard    |
|               | 
|---------------|  

|--------------------------|  
|        tableView         |
|--------------------------|               
|        textView          |
|--------------------------| 
|                          |
|        keyboard          |
|--------------------------|  

(キーボードが上になっていると上面図が消えていることがわかります)

これを行うためのコードは次のとおりです。

- (void)loadView
{
    [super loadView];

    // load top view
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, TOP_VIEW_HEIGHT)];
    _topView.autoresizingMask =  UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
    _topView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_topView];

    // load table view (at the middle)
    CGRect tableFrame = CGRectMake(0, TOP_VIEW_HEIGHT, self.view.bounds.size.width, self.view.bounds.size.height - TOP_VIEW_HEIGHT - TEXT_VIEW_HEIGHT);
    _tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    _tableView.autoresizingMask =  UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    // load text view (at the bottom)
    CGRect textViewFrame = CGRectMake(0, tableFrame.size.height + TOP_VIEW_HEIGHT, self.view.bounds.size.width, TEXT_VIEW_HEIGHT);
    _textView = [[UITextView alloc] initWithFrame:textViewFrame];
    _textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    _textView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_textView];
}  



- (void)keyboardWillShow:(NSNotification *)notification 
{
    NSDictionary *userInfo = [notification userInfo];

    // Get animation info from userInfo
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;


    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];

    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect textViewFrame = self.textView.frame;
    textViewFrame.origin.y -= kbSize.height;

    CGRect topViewFrame = self.topView.frame;
    topViewFrame.origin.y -= topViewFrame.size.height;

    CGRect tableFrame = self.tableView.frame;
    tableFrame.origin.y -= topViewFrame.size.height;
    tableFrame.size.height -= (kbSize.height - topViewFrame.size.height);

    [UIView animateWithDuration:animationDuration delay:0.0 options:animationCurve animations:^{
        self.textView.frame = textViewFrame;
        self.tableView.frame = tableFrame;
        self.topView.frame = topViewFrame;
    }completion:^(BOOL finished) {

    }];
}

- (void)keyboardWillHide:(NSNotification *)notification 
{
    NSDictionary *userInfo = [notification userInfo];

    // Get animation info from userInfo
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;


    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];

    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect textViewFrame = self.textView.frame;
    textViewFrame.origin.y += kbSize.height;

    CGRect topViewFrame = self.topView.frame;
    topViewFrame.origin.y += topViewFrame.size.height;

    CGRect tableFrame = self.tableView.frame;
    tableFrame.origin.y += topViewFrame.size.height;
    tableFrame.size.height += kbSize.height - topViewFrame.size.height;

    [UIView animateWithDuration:animationDuration delay:0.0 options:animationCurve animations:^{
        self.textView.frame = textViewFrame;
        self.tableView.frame = tableFrame;
        self.topView.frame = topViewFrame;
    }completion:^(BOOL finished) {
        //
    }];
}

私が望むもののほとんどはうまく機能します。問題は、キーボードが横向きモードで表示されている場合、問題が発生することです。

autoresizingMask現在設定しているかどうかわからないので、viewsプロパティに注意してください。

4

2 に答える 2

1

自動回転の場合は、トップビューのマスクをちょうど柔軟な幅に設定し、中央のマスクを柔軟な幅と柔軟な高さに設定し、テキストビュー、柔軟な幅と上余白を設定してみてください。それが問題なく機能するとは思えませんが、試してみる価値はあります。willAnimateRotationToInterfaceOrientation:duration:カスタム作業のためにViewControllerをオーバーライドできます。基本的な自動回転マスクはこの種のレイアウトを処理できないと確信していますが、間違っていることが証明されることを望んでいます。

loadViewまた、次のように向きを確認してください。

if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
    // do one layout
} else {
    // do another layout
}

私は自分のアプリでも非常にカスタムな回転作業を行っており(カスタムの分割ビューコントローラーをゼロから考えてください)、とを使用して行っていwillAutorotateますUIInterfaceOrientationIsLandscape

質問の言い方が原因で、これは当てはまらない場合があります。キーボードを開いてから横向きに回転したときに問題が発生するかどうかはわかりません(この場合、この回答が役立つ場合があります)。または、横向きでキーボードを開いたときに問題が発生するかどうか(この場合、この回答はあまり関係がない可能性があります)。

于 2012-06-06T21:14:45.337 に答える
1

問題が見つかりました。keyboardWillShow/willHideでビューの上下のアニメーションを実行したときに、キーボードの座標を画面からビューの座標に変換しませんでした。

CGRect kbRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect fromView:nil];
CGSize kbSize = kbRect.size; 

今では正常に動作しています:)

于 2012-06-06T21:37:34.473 に答える