Button.center不适用于Swift中的所有屏幕3/4

问题描述:

我在我的应用程序中创建了一个“更多”按钮,并且在我们关注按钮时创建了一个很酷的动画。它在iPhone 6上运行得非常好,但不是在6S plus或5s上运行。Button.center不适用于Swift中的所有屏幕3/4

这是我的按钮和我的约束:

enter image description here

我创建我动画一个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上,它完全在“更多”按钮上)。

enter image description here

我觉得buttonFocused.center值是从iPhone 6

值我能做些什么来解决这个问题?

+0

在哪里你称这个'createBackgroundEffet'? – Tj3n

+0

在视图的didLoad()或awakeFromNib()视图 – KevinB

+1

尝试调用它从'viewWillAppear'或'viewDidLayoutSubview',在'viewDidLoad'视图尚未布局,导致错误的行为 – Tj3n

你肯定有一些问题与你的约束。

首先是去除所有限制,这限制 enter image description here

比分配这样 *家伙约束: enter image description here

和更多的按钮:

enter image description here

+0

感谢您的答案,但我不能这样做。图片的高度和宽度不固定。它根据屏幕设置:/ – KevinB

+0

您是否尝试将它们放入堆栈视图并使用大小类?我认为它应该可以帮助你。 – swift2geek