36

インスタンス内のプレーンである、UITextViewを埋めたいのですが。superViewUIViewUIViewController

UITextViewのAPI指定のプロパティだけを使用してこれを行うことはできないようです。ここに示すようにこれらを設定しても何も起こりません。画面いっぱいに表示されても、小さいままです。autoresizingMaskautoresizesSubviewsUITextViewsuperView

// use existing instantiated view inside view controller;
// ensure autosizing enabled
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|
                             UIViewAutoresizingFlexibleWidth;
// create textview
textView = [[[UITextView alloc] autorelease] initWithFrame:CGRectMake(0, 0, 1, 1)];
// enable textview autoresizing
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
                              UIViewAutoresizingFlexibleHeight];
// add textview to view
[self.view addSubview:textView];

ただし、View Controller内で独自のビューをインスタンス化し、その'.view'プロパティを置き換えると、すべてが期待どおりに機能し、textViewがそのスーパービューを埋めます。

// reinstantiate view inside view controller
self.view = [[UIView alloc]init];
// create textview
textView = [[[UITextView alloc] autorelease] initWithFrame:CGRectMake(0, 0, 1, 1)];
// enable textview autoresizing
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
                              UIViewAutoresizingFlexibleHeight];
// add textview to view
[self.view addSubview:textView];

これらすべてのイニシャライザー/メソッド内で両方のコードチャンクを試しましたが、すべての場合に同じ状況が発生します。

-(id)init;
-(id)initWithFrame:(CGRect)frame;
-(void)viewDidLoad;

の「.view」を復元するのUIViewControllerは醜いことだと思いますが、私が間違っていることを誰かが説明できますか?最初のフレーム設定コードを1回UIViewControllerサイズ変更することで問題を解決できUITextView、その後autoresizingは希望どおりに動作すると思いました。

-(void)viewDidLoad {
    textView.frame = self.view.frame;
}

...しかし、この段階ではview.frameが設定されていないようです。また、'。size'値が定義されていないため、textViewは小さいままです。

私が望むことを達成するための適切な方法は何ですか?UITextView:initWithFrameスーパービューを埋めるために、を介してフルスクリーンのサイズを明示的に指定する必要がありますか?

アドバイスをいただければ幸いです。

4

2 に答える 2

89

自動サイズ変更は、サブビューがそのスーパービューのサイズを占めることを意味するものではありません。これは、スーパービューの境界が変更されるたびに、スーパービューのサイズ変更に応じてサイズが変更されることを意味します。したがって、最初に、サブビューのサイズを正しい値に設定する必要があります。自動サイズ変更マスクは、将来のサイズ変更に対応します。

必要なのはこれだけです。

textView = [[[UITextView alloc] autorelease] initWithFrame:self.view.bounds];
[textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|
                              UIViewAutoresizingFlexibleHeight];
于 2011-02-07T12:35:27.797 に答える
0

そしてここでSwift3ソリューション:

myView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
于 2017-04-19T08:06:06.757 に答える