如何在Swift中以编程方式布局约束时包含第三个变量?

时间:2016-07-27 15:07:05

标签: ios swift

Two buttons in UIView

我基本上有两个按钮放在一个较大的视图中,我希望按钮之间的距离等于较大视图的倍数。例如,我想做类似的事情:

var buttonOneRightConstraint = NSLayoutConstraint(item: button1, attribute: .Right, relatedBy: .Equal, toItem: button2, attribute: .Left, multiplier: 1.0, constant: -largerView.frame.width/10)

在一个方向上效果很好,但在视图大小更改时不会更新(例如,设备被旋转)。

我想我希望每次更新视图时都计算largeView.frame.width,而不是将其视为常量,但不知道如何做到这一点!

1 个答案:

答案 0 :(得分:0)

//use browser sniffing to determine if IE or Opera (ugly, but required) var isOpera, isIE = false; if (typeof(window.opera) != 'undefined') { isOpera = true; } if (!isOpera && navigator.userAgent.indexOf('Internet Explorer')) { isIE = true; } //fix both IE and Opera (adjust when they implement this method properly) if (isOpera || isIE) { document.nativeGetElementById = document.getElementById; //redefine it! document.getElementById = function(id) { var elem = document.nativeGetElementById(id); if (elem) { //verify it is a valid match! if (elem.attributes['id'] && elem.attributes['id'].value == id) { //valid match! return elem; } else { //not a valid match! //the non-standard, document.all array has keys for all name'd, and id'd elements //start at one, because we know the first match, is wrong! for (var i = 1; i < document.all[id].length; i++) { if (document.all[id][i].attributes['id'] && document.all[id][i].attributes['id'].value == id) { return document.all[id][i]; } } } } return null; }; } function isThereAnyVisibleElement(arrElements) { var ret = false; for (var index in arrElements) { if (isNaN(index)) continue; var elem = arrElements[index]; if (elem.style.display !== 'none') { ret = true; break; } } return ret; } function groep(sel) { var display = (sel.value === "Nee") ? 'table-cell' : 'none'; var trElement = sel.parentNode.parentNode; var th = document.getElementById('th_aantal_pallets_n_oke'); var td = trElement.querySelector('.hallo1'); td.style.display = display; if (isThereAnyVisibleElement(document.querySelectorAll('.hallo1'))) display = 'table-cell'; th.style.display = display; } 方法中添加设备方向更改事件的观察者:

didFinishLaunchingWithOptions

然后,计算 NSNotificationCenter.defaultCenter().addObserver(TheClassYouWantTheMethodToBeFired, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil) 新方法

中的布局

How to detect orientation change?