gpt4 book ai didi

ios - UIBarButtonItem Frame 在旋转动画后变化

转载 作者:行者123 更新时间:2023-11-28 07:59:31 24 4
gpt4 key购买 nike

我在自定义 UIBarButtonItem 中执行旋转动画时遇到框架问题。 enter image description here

我的代码如下:

类 View Controller :UIViewController {

var bellIcon: UIBarButtonItem = UIBarButtonItem()

override func viewDidLoad() {
super.viewDidLoad()
setupBellButton()
}

func setupBellButton(){
let icon = UIImage(named: "Bell.png")
let iconSize = CGRect(origin: CGPoint.zero, size: icon!.size)
let iconButton = UIButton(frame: iconSize)
iconButton.setBackgroundImage(icon, for: .normal)

let badgeView = UIView(frame: CGRect(origin: CGPoint(x: iconButton.frame.maxX-10, y: iconButton.frame.minX), size: CGSize(width: 10, height: 10)))
badgeView.layer.cornerRadius = 5
badgeView.layer.borderColor = UIColor.red.cgColor
badgeView.backgroundColor = UIColor.red
badgeView.layer.masksToBounds = true
badgeView.clipsToBounds = true

let btnContainer = UIView(frame: CGRect(origin: CGPoint.zero, size: icon!.size))
btnContainer.addSubview(iconButton)
btnContainer.addSubview(badgeView)

badgeView.transform = CGAffineTransform(scaleX: 0, y: 0)

UIView.animate(withDuration: 1) {
badgeView.transform = CGAffineTransform(scaleX: 1, y: 1)
}
bellIcon.customView = btnContainer

iconButton.addTarget(self, action:#selector(bellIconTapped), for: .touchUpInside)

navigationItem.rightBarButtonItem = bellIcon
bellIcon.customView?.backgroundColor = UIColor.purple
}

@IBAction func bellIconTapped(_ sender: Any) {
UIView.animate(withDuration: 0.3, animations: {

self.bellIcon.customView!.transform = CGAffineTransform(rotationAngle: -0.4)
}) { (true) in
UIView.animate(withDuration: 0.3, animations: {

self.bellIcon.customView!.transform = CGAffineTransform(rotationAngle: 0.4)
}, completion: { (true) in
UIView.animate(withDuration: 0.4, animations: {
self.bellIcon.customView!.transform = CGAffineTransform.identity

})
})
}
}

提前致谢!!!

最佳答案

我通过简单地将常量添加到 'bellIcon.customView'

解决了 iOS 10 和 11 的上述问题

我在'setupBellButton()'

的末尾添加了以下代码
    bellIcon.customView?.translatesAutoresizingMaskIntoConstraints = false

let widthConstraint = bellIcon.customView?.widthAnchor.constraint(equalToConstant: 25.0)
let heightConstraint = bellIcon.customView?.heightAnchor.constraint(equalToConstant: 25.0)

bellIcon.customView?.addConstraints([widthConstraint!, heightConstraint!])

编码愉快!!!

关于ios - UIBarButtonItem Frame 在旋转动画后变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47038613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com