当我从纵向模式更改为横向时,按钮背景图像消失

问题描述 投票:0回答:1

我已经在我的按钮上插入了一个背景图像,使它看起来整洁。

但是当我将设备从纵向模式旋转到横向模式时,按钮的背景图像会消失。

附图供参考。

ButtonBackgroundPortrait

ButtonBackgroundLandscape

ios swift uibutton
1个回答
0
投票

在你的情况下,问题是按钮被压入height = 0,因为你对按钮有这两个约束:

clickHere.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)

clickHere.bottomAnchor.constraint(equalTo: self.bottomLayoutGuide.topAnchor, constant: 304)

在横向中,按钮的centerY比304 + buttonHeight更靠近底部,因此autolayout按下按钮到height = 0,以尽可能接近满足约束。由于按钮的高度由按钮的固有大小设置,因此其优先级低于约束的优先级。

在您的情况下,解决方案非常简单,只需删除第二个约束:

clickHere.bottomAnchor.constraint(equalTo: self.bottomLayoutGuide.topAnchor, constant: 304)

在故事板的屏幕截图中,它位于约束列表的底部,其名称是

底部布局指南.top =单击Here.bottom + 304

© www.soinside.com 2019 - 2024. All rights reserved.