- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 iOS 应用程序,有 3 个容器 View (A、B 和 C)。顶部容器 View (A) 是静态的,下面有一个容器 View (B),带有一个按钮,可将其“移动”到一侧以显示第三个容器 View (C)。我遇到的问题是,当我从 B 移动到 C,然后向侧面翻转手机时,B 和 C 重叠,如果我按下移动按钮,它们不会保留其位置,并且位置会突然重叠,因为它们都是重叠后,它们都走出了屏幕。我添加了 3 个屏幕截图来显示该行为:
https://imgur.com/a/wluCkpT
到目前为止,这是我在 viewDidLoad 上的设置,我调用了 resetPositions():
class HomeVC: UIViewController{
var isMainContainer: Bool = true
func resetPositions() {
if isMainContainer {
if UIApplication.shared.statusBarOrientation.isLandscape {
containerViewBot.center.y = containedView.center.y
containerViewBotBack.center.y = containedView.center.y - offset
} else {
containerViewBot.center.x = self.view.center.x
containerViewBotBack.center.x = self.view.center.x - offset
}
} else {
if UIApplication.shared.statusBarOrientation.isLandscape {
containerViewBotBack.center.y = containedView.center.y
containerViewBot.center.y = containedView.center.y + offset
} else {
containerViewBotBack.center.x = self.view.center.x
containerViewBot.center.x = self.view.center.x + offset
}
}
}
现在,当我按下按钮从一个 View “移动”到另一个 View 时,“isMainContainer”会发生变化:
func getMore(from: String) {
//from is the sender
if from == "homeResults" {
containerViewBotBack.isHidden = false
goToCommute()
} else {
//containerViewBotBack.isHidden = true
containerViewBot.isHidden = false
goToResults()
}
}
最后是 goToCommute 和 goToResults 方法:
func goToCommute() {
let duration = 0.5
if UIApplication.shared.statusBarOrientation.isLandscape {
UIView.animate(withDuration: duration, animations: {
self.moveUp(view: self.containerViewBot)
self.moveUp(view: self.containerViewBotBack)
}) { (_) in
self.containerViewBotBack.isHidden = false
self.containerViewBot.isHidden = true
}
} else {
UIView.animate(withDuration: duration, animations: {
self.moveRight(view: self.containerViewBot)
self.moveRight(view: self.containerViewBotBack)
}) { (_) in
self.containerViewBotBack.isHidden = false
self.containerViewBot.isHidden = true
}
}
isMainContainer = false
}
func goToResults() {
let duration = 0.5
if UIApplication.shared.statusBarOrientation.isLandscape {
UIView.animate(withDuration: duration, animations: {
self.moveDown(view: self.containerViewBot)
self.moveDown(view: self.containerViewBotBack)
}) { (_) in
self.containerViewBot.isHidden = false
self.containerViewBotBack.isHidden = true
}
} else {
UIView.animate(withDuration: duration, animations: {
self.moveLeft(view: self.containerViewBot)
self.moveLeft(view: self.containerViewBotBack)
}) { (_) in
self.containerViewBot.isHidden = false
self.containerViewBotBack.isHidden = true
}
}
isMainContainer = true
}
我在覆盖 viewWillTransition(...) 上调用“resetPositions()”命令
最佳答案
您可以通过以下方式检测设备旋转并重新定位您的项目。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(deviceRotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
func deviceRotated(){
if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
print("Landscape")
// Resize other things
}
if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
print("Portrait")
// Resize other things
}
}
关于ios - 在 viewWillTransition 上重新定位 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54264367/
我正在制作自定义键盘。我想根据 View 的宽度和高度生成键(UIButtons)的宽度和高度。 最初加载键盘时,viewDidAppear 会正确确定 View 的高度。 (375 x 216) o
我正在使用 UICollectionView 创建全屏图片库。当用户旋转设备时,我会在 中对 UICollectionView 执行更新 func viewWillTransition(调整大小:CG
我有一个 iOS 应用程序,有 3 个容器 View (A、B 和 C)。顶部容器 View (A) 是静态的,下面有一个容器 View (B),带有一个按钮,可将其“移动”到一侧以显示第三个容器 V
我正在使用具有两种 View 类型的日历 cocoapod(CVCalendar:https://github.com/CVCalendar/CVCalendar);周 View 和月 View 。
我正在学习开发 iOS 应用程序,目前我有 4 个 View Controller 。 在第一个 viewController 中,我使用方法 viewWillTransition 在运行时调整 UI
我使用 viewWillTransition 来更改 View 的框架,如果我的 ViewController 位于最顶层,那么一切都很好,这是旋转时打印的框架 TEST frame: (0.0, 0
我以编程方式在我的 UIInputViewController 的 viewDidLoad() 中创建了一组按钮。当方向从横向变为纵向时,我试图更改这些按钮的大小,反之亦然。 我正在尝试使用 over
我试图仅用一个 View 更改方向,其余 View 都固定为纵向。我在 AppDelegate 中设置了一个方法,如下所示 func application(_ application: UIAppl
我的应用程序是基于选项卡的导航。我在 iOS11 中使用 Xcode9。为了支持横向/纵向兼容性,我在所有 viewController 中使用以下方法。 override func viewWill
我必须检查我的设备在 iOS 8+ 中是否改变了方向。 我的做法是: override func viewWillTransition(to size: CGSize, with coordinato
这是我如何重写我的 View Controller ,但是,如果有另一种更好的方法来做到这一点,那么我的第二个 View Controller 不符合重写请指导我,任何建议都会很好. override
我有一个名为 myView 的 UIView 子类,当设备方向发生变化时,它应该重新配置自身。我在 UIViewController 中使用 viewWillTransition(to size: C
我正在制作一个 iMessage 应用程序。我已经实现了 viewWillTransition(to:with:) 以调整/重新排列我的应用程序中的 View 。当我的应用程序的 presentati
我是一名优秀的程序员,十分优秀!