0

1 週間以上もこれに固執しているなんて信じられません。

私はこれに関する他のすべてのスタックの質問/回答を見ました(そして試しました)が、どれもうまくいきませんでした。

基本的に、私はツールバーを備えた詳細ビューを持っており、その下にUITextView残りのスペースを占め、スーパービューの端までの端に20ポイントの境界線を残しています。

私が必要とするのは、キーボードが表示されているときに、テキストビューがフレームまたはコンテンツインセットのものを変更することだけです。これにより、キーボードが何も覆わず、テキストがテキストの最後までスクロールし、新しい入力/行の折り返しが行われますキーボードによって隠されていません - 簡単ですよね?ええと...いいえ。

ユーザーが変更された場合Orientation(4 つすべてがサポートされています)、対応するように調整する必要があります。

次に、キーボードを閉じると、フルサイズに戻る必要があります(可能な新しい向きに応じて)。

Autolayoutこれは、両方で行った最初のプロジェクトですiOS 7(そして、新しいテキスト行をテキスト ビューの下部の「下」に配置するというバグに遭遇しましたiOS7が、スタック修正のおかげで、今は問題ありません。)

ただし、このサイトで試したソリューションはどれも機能しません。

UITextViewIB に配置し、portrait[提案された制約にリセット] を選択して、の制約を設定しています。キーボードが表示されていない場合、4 つの方向すべてに対して正しくレイアウトされているようです。

4

3 に答える 3

1

キーボードが表示されたり消えたりするときに、制約をビューの下部に調整することで実行できます。以下の例では、ツールバーとテキスト ビューを備えたビュー コントローラーがあります。テキスト ビューには、メイン ビューの下部と側面に対する制約 (値 20) と、ビューの上部にあるツールバーに対する制約があります。ビューの下部への制約に対する IBOutlet があります。keyboardWillShow: メソッドでは、制約の定数値を正しく取得するために、ビューの向きを確認する必要があることに注意してください。ランドスケープ モードでは、キーボードの幅と高さが逆になります (つまり、サイズとして得られるもの)。 width は実際には高さであり、size.height は幅を示します)。

@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomCon; // outlet to the constraint between the text view and the bottom of the view
@property (weak,nonatomic) IBOutlet UITextView *tv; // outlet for the text view
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}



- (void)keyboardWillShow:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    if (self.view.bounds.size.width < self.view.bounds.size.height) {
        self.bottomCon.constant = kbSize.height + 20;
    }else{
        self.bottomCon.constant = kbSize.width + 20;
    }
}


-(void)keyboardDidShow:(NSNotification *) aNotificaation {
    [self.tv scrollRangeToVisible:NSMakeRange(self.tv.text.length - 1, 1)];
}


- (void)keyboardWillHide:(NSNotification*)aNotification {
    self.bottomCon.constant = 20;
}

-(IBAction)finishedEditing:(id)sender { // action for "Done" button on tool bar
    [self.tv resignFirstResponder];
}
于 2013-11-02T05:56:59.433 に答える
0

上記の信じられないほど役立つ rdelmar からの提案されたコード修正は、縦向きで完全に機能します。ただし、ランドスケープに変更すると、次のクラッシュが発生します。

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x941da50 V:|-(158)-[UITextView:0x9926000]   (Names: '|':UIView:0x9449a30 )>",
    "<NSLayoutConstraint:0x9449a00 V:[UITextView:0x9926000]-(1044)-|   (Names: '|':UIView:0x9449a30 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x97b2d70 h=--& v=--& V:[UIView:0x9449a30(768)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9449a00 V:[UITextView:0x9926000]-(1044)-|   (Names: '|':UIView:0x9449a30 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

ここで、詳細ビューから EVERY 制約をクリアし、最初からやり直しました。UILabel、UIToolBar、および UITextView はすべて、先頭、末尾、および上端が固定されています。

UITextView にも BOTTOM EDGE PINNED がありました。

そして、この下部の制約は、上記の投稿で言及されている IBOutlet にリンクされている制約です。

何がうまくいかないのですか?

于 2013-11-02T16:50:08.043 に答える
0
CGSize kbSize       = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

if ( kbSize.height < kbSize.width )

これは、rdelmar による上記の回答に必要な唯一の変更でした。本当にありがとう!これは私を1週間怒らせました!!!!

于 2013-11-03T00:14:01.553 に答える