[英]Orientation change in Swift height change and uibutton hide on landscape
我正在制作一個自定義鍵盤,其中當移動設備的方向改變時,然后在橫向中,我必須隱藏一個按鈕,請建議我該怎么做?我正在使用下面的代碼來完成此任務,請幫助我。 在風景中該按鈕也是可見的,我想在風景中隱藏按鈕,在肖像模式下可見
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
var currentDevice: UIDevice = UIDevice.currentDevice()
var orientation: UIDeviceOrientation = currentDevice.orientation
if orientation.isLandscape {
button.hidden = true
}
if orientation.isPortrait {
button.hidden = false
}
}
在您的viewDidLoad
NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged", name: UIDeviceOrientationDidChangeNotification, object: nil)
然后添加此方法
func orientationChanged()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
button.hidden = true
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
button.hidden = false
}
}
注意:
UIDevice.currentDevice().orientation
可能無法為您提供正確的方向。 您可以使用狀態欄方向來代替UIApplication.sharedApplication().statusBarOrientation
從蘋果文檔:
UIDevice.currentDevice().orientation
該屬性的值是一個常數,指示設備的當前方向。 該值代表設備的物理方向,並且可能與應用程序用戶界面的當前方向不同。 有關可能值的說明,請參見“ UIDeviceOrientation”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.