Button.center不适用于Swift中的所有屏幕3/4
问题描述:
我在我的应用程序中创建了一个“更多”按钮,并且在我们关注按钮时创建了一个很酷的动画。它在iPhone 6上运行得非常好,但不是在6S plus或5s上运行。Button.center不适用于Swift中的所有屏幕3/4
这是我的按钮和我的约束:
我创建我动画一个backgroundView当我焦点的按钮:
func createBackgroundEffet(_ backgroundView: UIView, size: Int, buttonFocused: UIButton){
backgroundView.frame = CGRect(x: 0, y: 0, width: size, height: size)
backgroundView.backgroundColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1)
backgroundView.layer.cornerRadius = backgroundView.frame.size.width/2
backgroundView.clipsToBounds = true
backgroundView.center = buttonFocused.center
backgroundView.alpha = 1
self.insertSubview(backgroundView, at: 2)
}
(I设置backgroundView.alpha = 1例如)
而这个结果取决于屏幕(在iPhone 6上,它完全在“更多”按钮上)。
我觉得buttonFocused.center值是从iPhone 6
值我能做些什么来解决这个问题?
答
+0
感谢您的答案,但我不能这样做。图片的高度和宽度不固定。它根据屏幕设置:/ – KevinB
+0
您是否尝试将它们放入堆栈视图并使用大小类?我认为它应该可以帮助你。 – swift2geek
在哪里你称这个'createBackgroundEffet'? – Tj3n
在视图的didLoad()或awakeFromNib()视图 – KevinB
尝试调用它从'viewWillAppear'或'viewDidLayoutSubview',在'viewDidLoad'视图尚未布局,导致错误的行为 – Tj3n