我的子視圖有問題。我有2個可見的按鈕和1,它應該顯示後點擊第一個。更準確地說,我有開始按鈕,重置按鈕和停止按鈕。加載時應該只顯示「開始」和「重置」按鈕,但是當我按下開始按鈕時,應該隱藏「重置」按鈕並顯示停止按鈕。像isHidden這樣的語法不起作用。問題是什麼?隱藏和顯示(子視圖)按鈕或更改標題?
星,停止和復位按鈕:
var stopButton: UIButton{
let stopButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100))
stopButton.backgroundColor = .white
stopButton.setTitle("Stop", for: .normal)
stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside)
stopButton.layer.cornerRadius = 50
stopButton.layer.masksToBounds = true
stopButton.isHidden = true
return stopButton
}
var resetButton: UIButton{
let resetButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100))
resetButton.backgroundColor = .red
resetButton.setTitle("Reset", for: .normal)
resetButton.addTarget(self, action: #selector(resetButtonAction), for: .touchUpInside)
resetButton.layer.cornerRadius = 50
resetButton.layer.masksToBounds = true
return resetButton
}
var startButton: UIButton{
let startButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
startButton.backgroundColor = .green
startButton.setTitle("Start", for: .normal)
startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside)
startButton.layer.cornerRadius = 50
startButton.layer.masksToBounds = true
return startButton
}
在這裏,我添加此子視圖(本功能也添加UIView的,這就是所謂的viewDidLoad中):
func addBottomView()
{
let orginX = (collectionView?.frame.minX)!
let orginY = (collectionView?.frame.maxY)!
let heightOfView = view.frame.height - (view.frame.height/100) * 70
let bottomView = UIView(frame: CGRect(x: orginX, y: orginY, width: view.frame.width, height: heightOfView))
bottomView.backgroundColor = .orange
view.addSubview(bottomView)
bottomView.addSubview(stopButton)
bottomView.addSubview(startButton)
bottomView.addSubview(resetButton)
}
這裏是我的功能,這是連接到這個按鈕:
func startButtonAction()
{
blinkAction()
print("START ACTION")
resetButton.isHidden = true
stopButton.isHidden = false
view.layoutSubviews()
}
func resetButtonAction()
{
print("RESET ACTION")
blinkAction()
}
func stopButtonAction()
{
print("STOP ACTION")
blinkAction()
resetButton.isHidden = false
stopButton.isHidden = true
view.layoutSubviews()
}
我添加layoutSubviews方法,但它沒有幫助。我也嘗試使用隱藏的按鈕名稱之前,但我也不工作。有什麼建議?
計算的特性創建按鈕,爲什麼你在STOPBUTTON計算財產使用「stopButton.isHidden =真」? –
因爲在加載停止按鈕應該被隱藏。只有點擊開始按鈕後 –