2016-10-17 8 views
1

ストーリーボードにUIButtonが追加されました。下部、左、右がスーパービューに設定されています。高さはハードコードされた60に設定されます。スーパービューの幅で、下部に配置されます。レイアウト制約が機能していません

スーパービューに別のビューを追加して合計2つのビューを表示しようとしています。私はこの見方が残りの空間を満たすことを望みます。私はしかし、効果はボタンが画面全体を占めており、私はすべての新しいビューを見ることができないということです、

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    locationManager = LocationManager() 

    webview = WKWebView() 
    webview.backgroundColor = .blue 
    webview.translatesAutoresizingMaskIntoConstraints = true 
    webview.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin] 
    view.addSubview(webview) 

    var constraints: [NSLayoutConstraint] = ([.top, .width] as [NSLayoutAttribute]).map { 
     NSLayoutConstraint(item: webview, attribute: $0, relatedBy: .equal, toItem: view, attribute: $0, multiplier: 1, constant: 0) 
    }; 
    constraints.append(NSLayoutConstraint(item: webview, attribute: .bottom, relatedBy: .equal, toItem: connectionButton, attribute: .top, multiplier: 1, constant: 0)) 

    view.addConstraints(constraints) 
    NSLayoutConstraint.activate(constraints) 
} 

をこのコードをしようとしています。私は間違って何をしていますか?

+0

インタフェースビルダーでこれをやっていない理由はありますか? – Adrian

+2

WKWebView(webview)は利用できません。 –

答えて

2

あなたは近いですが、キー物事のカップルが不足して:あなた自身の制約を作成する場合

  1. 、あなたが自動サイズ変更マスクの制約を使用したくないので、webview.translatesAutoresizingMaskIntoConstraintsを次のように設定falseで、autoresizingMask行は必要ありません。

  2. 幅をビューと同じに設定しますが、x位置は定義しませんでした。また、前縁または水平中心を制約して位置を定義することもできます。

私はあなたの問題を解決すると思います。

-1

私が正しく理解していれば、このようなことが働く可能性があります。 WebViewはボトム60以外のすべてのスペースを占有します。左と右の2つのボタンはボトム60にあります。左に20マージン、幅60に10スペースあります。

webView.translatesAutoresizingMaskIntoConstraints = false 
buttonLeft.translatesAutoresizingMaskIntoConstraints = false 
buttonRight.translatesAutoresizingMaskIntoConstraints = false 

addSubview(webView) 
addSubview(buttonLeft) 
addSubview(buttonRight) 

// setup your views 

let viewsDict = [ 
    "web" : webView, 
    "btnL" : buttonLeft, 
    "btnR" : buttonRight 
] 

addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[web]-60-|", options: [], metrics: nil, views: viewsDict)) 
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[web]|", options: [], metrics: nil, views: viewsDict)) 

addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[email protected][btnL(60)]|", options: [], metrics: nil, views: viewsDict)) 
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[email protected][btnR(60)]|", options: [], metrics: nil, views: viewsDict)) 
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[btnL(60]-10-[btnR(60)][email protected]|", options: [], metrics: nil, views: viewsDict)) 
+0

申し訳ありませんが、これはストーリーボード以外のソリューションです:/あなたはプログラムですべてを行うことができます;) – budidino

関連する問題