2017-01-31 19 views
0

WKWebViewをself.viewのすべての側面に固定するだけで、回転に関係なく常に可能な限り伸びます。WKWebViewの制約が機能しない

-(void)viewWillAppear:(BOOL)animated { 
    [super viewDidLoad]; 
    self.title = @"Worship Slides"; 

    self.productURL = @"http://www.316apps.com/Fritch/worship.key"; 

    NSURL *url = [NSURL URLWithString:self.productURL]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]; 

    _theWorship = [[WKWebView alloc] initWithFrame:self.view.frame]; 
    [_theWorship setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [_theWorship loadRequest:request]; 
    _theWorship.frame = CGRectMake(0, 0, self.navigationController.view.bounds.size.width, self.navigationController.view.bounds.size.height); 
    [self.view addSubview:_theWorship]; 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_theWorship attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]]; 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_theWorship attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]]; 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_theWorship attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]]; 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_theWorship attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]]; 
} 
+1

'viewWillAppear'で' super viewDidLoad'を呼び出さないでください。あなたは 'superviewWillAppear'を呼び出す必要があります!そうでなければ、あなたのコードはうまく見えます(2つの 'frame'設定が無意味であり、削除する必要がありますが、害はありません)。ウェブビューが消えていますか?回転を行い、View Debuggerを使用してWebビューの実際の位置を確認します。 – matt

+0

あなたは[super viewDidLoad]を呼び出すtypoを持っています - 本当に正しいはずです。この関数は '-viewDidLoad'でなければなりません。 –

+0

@matt viewWillAppearでこれをしてはいけません。これはviewDidLoadで行うべきです。 –

答えて

0
-(void)viewWillAppear:(BOOL)animated { 
    **[super viewDidLoad];** 

変更:

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

をまた、あなたを確認して、次のコードを使用して、それが最初の回転はあるが、回転させた後、それは単に、すべてが消え何のためのビューを記入しますコードがすでに呼び出されているかどうかをチェックするメソッドを追加します。そうでなければ、ビューが表示されるたびに複数回呼び出されます。代わりにviewDidLoadメソッドから呼び出す必要がありますが、複数回呼び出さなければ何でも選択できます。

はまた、あなたが呼び出すことができるすべてを追加した後:

[_theWorship layoutIfNeeded]; 
0

をaddConstraintを取り除く、代わりに真=のisActiveを呼び出します。ドキュメントを参照してください:

When developing for iOS 8.0 or later, set the constraint’s active property to true instead of calling the addConstraint(_:) method directly. The isActive property automatically adds and removes the constraint from the correct view.

またNSLayoutAnchorを使用します。それはNSLayoutConstraintほど長くはありません。私はループでNSLayoutConstraintを使うか、NSLayoutAnchorで制約を表すことができないとき(つまり、乗算する)。

関連する問題