簡體   English   中英

Swift 更改按鈕 position 不起作用

[英]Swift changing button position doesn't work

我正在嘗試更改 UIButton,但它不起作用。 我的代碼是這樣的。

@IBAction func addButtonTapped(_ sender: Any) {
    print(defaultAddButton.center)
    let buttonPos = CGPoint(x: defaultAddButton.frame.midX, y: defaultAddButton.frame.midY)
    let width = self.view.bounds.width
    let height: CGFloat = 100
    let uiView = SubCollecionView(frame: CGRect(x: buttonPos.x - width / 2, y: buttonPos.y - height / 2, width: width, height: height))
    self.view.addSubview(uiView)
    
    self.defaultAddButton.center.y += 200
}

SubCollectionView 是我的自定義視圖。 上面的代碼在沒有 self.view.addSubview(uiView) 行的情況下工作。 我不明白為什么它不起作用。 先感謝您!

您可以嘗試在視圖中設置更改/框架

viewDidLayoutSubviews

您應該在此之后調用layoutIfNeeded()

@IBAction func addButtonTapped(_ sender: Any) {
        print(defaultAddButton.center)
        let buttonPos = CGPoint(x: defaultAddButton.frame.midX, y: defaultAddButton.frame.midY)
        let width = self.view.bounds.width
        let height: CGFloat = 100
        let uiView = SubCollecionView(frame: CGRect(x: buttonPos.x - width / 2, y: buttonPos.y - height / 2, width: width, height: height))
        self.view.addSubview(uiView)
        self.defaultAddButton.center.y += 200
        self.view.layoutIfNeeded()
    }

如果這不起作用。 請嘗試在主隊列中調用 UI 修改代碼。

@IBAction func addButtonTapped(_ sender: Any) {
        print(defaultAddButton.center)
        DispatchQueue.main.async {
            let buttonPos = CGPoint(x: defaultAddButton.frame.midX, y: defaultAddButton.frame.midY)
            let width = self.view.bounds.width
            let height: CGFloat = 100
            let uiView = SubCollecionView(frame: CGRect(x: buttonPos.x - width / 2, y: buttonPos.y - height / 2, width: width, height: height))
            self.view.addSubview(uiView)
            self.defaultAddButton.center.y += 200
            self.view.layoutIfNeeded()
        }
    }

不完全理解你的問題,但可以建議你有問題 animation,用這行代碼:

self.defaultAddButton.center.y += 200

上面的代碼不起作用,因為您需要更新視圖以立即更新其布局,使用此方法更新視圖“ layoutIfNeeded()

代碼示例:

class VC: UIViewController {
lazy var buttonAction: UIButton = {
        let view = UIButton()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.setTitle("Action", for: .normal)
        view.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
        view.backgroundColor = .blue
        view.addTarget(self, action: #selector(action(_:)), for: .touchUpInside)
        return view
    }()
    
    @objc func action(_ sender: UIButton) {
        print("action")
        print(buttonAction.center)
        
        let buttonPos = CGPoint(x: buttonAction.frame.midX, y: buttonAction.frame.midY)
            let width = self.view.bounds.width
            let height: CGFloat = 100
            let uiView = MediaPlayer(frame: CGRect(x: buttonPos.x - width / 2, y: buttonPos.y - height / 2, width: width, height: height))
            self.view.addSubview(uiView)
        self.buttonAction.center.y += 200
        UIView.animate(withDuration: 1.0) {
            self.view.layoutIfNeeded()
        }
    }
    
    func setupButton() {
        self.view.addSubview(buttonAction)
        buttonAction.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        buttonAction.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant:  -70).isActive = true
        buttonAction.heightAnchor.constraint(equalToConstant: 50).isActive = true
        buttonAction.widthAnchor.constraint(equalToConstant: 100).isActive = true
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .gray
        setupButton()
        
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM