简体   繁体   中英

Swift changing button position doesn't work

I'm trying to change UIButton but it doesn't work. My code is like this.

@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 is my custom view. Above code works without self.view.addSubview(uiView) line. I can't understand why it doesn't work. Thank you in advance!

You can try setting changes/frame in view over

viewDidLayoutSubviews

You should call layoutIfNeeded() after this.

@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()
    }

If this doesn't work. Please try to call UI modification code inside the main queue.

@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()
        }
    }

do not fully understand your problem, but can suggest that you have a problem with animation, with this line of code:

self.defaultAddButton.center.y += 200

The above code doesn't work because you need to update the view to update its layout immediately, use this method to update view " layoutIfNeeded() "

Example of code:

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()
        
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM