0
我正在使用Swift自動佈局,並試圖編寫一些代碼,直接在用戶按下的按鈕上添加一個按鈕。但是,由於我使用的是自動佈局,並且所有按鈕都是以編程方式創建的,以根據屏幕大小和方向調整其大小,所以我使用的代碼僅返回其原始值。如何使用自動佈局和約束條件在swift中找到frame.origin按鈕的x和y座標?
只是要清楚,「按鈕」是用戶正在按下,我試圖將button2添加到視圖直接出現在「按鈕」上方。
當前button2出現在20,20,其中按鈕的約束前的原始值和自動佈局被編程應用。
我的代碼是:
let button = sender as UIButton
// Tried this but does not work: self.view.layoutIfNeeded()
// Also this but this also does not work: button.setNeedsUpdateConstraints()
let button2 = UIButton.buttonWithType(.System) as UIButton
button2.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y - button.frame.height, button.frame.width, button.frame.height)
button2.sizeToFit()
button2.titleLabel?.font = UIFont.systemFontOfSize(15)
button2.setTranslatesAutoresizingMaskIntoConstraints(false)
button2.setTitle(button.titleLabel?.text, forState: .Normal)
button2.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
self.view.addSubview(button2)
感謝您的幫助,
感謝這個答案 - 真的幫助 – agf119105 2014-10-05 20:37:37