2017-10-31 15 views
1

カスタムUIBarButtonItemで回転アニメーションを実行中にフレーム内に問題が発生しています。 enter image description hereUIBarButtonItem回転アニメーション後のフレームの変更

私のコードは次の通りである:

クラスのViewController:の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 

      }) 
     }) 
    } 
} 

}予め

おかげ! !

+0

これがiOS 11でのみ発生している場合は、https://stackoverflow.com/q/46203703/5442445またはhttps://stackoverflow.com/q/46247451/5442445 – beyowulf

+0

にお問い合わせください。@beyowulf iOS 10でも – chetan

答えて

0

はI「)は、(setupBellButton」単にの終わりに「bellIcon.customView」

I追加した次のコードに定数を追加することによって、両方のiOS 10及び11については、上記問題を解決しました

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!]) 

ハッピーコーディング!!!

関連する問題