我正在构建类似书籍阅读器的东西。当用户旋转手机时我想增加字体大小。我正在使用一个UITableView
显示文本块。
问题是,增加字体大小会增加表格视图中行的高度,如果我在纵向模式下阅读第 320 段,我会在横向模式下得到 280 或类似的内容。
我使用以下代码设置了一个轮换通知侦听器:
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:device];
并尝试在旋转之前保存最后一段索引,然后在旋转后向下滚动到它,但我似乎无法达到预期的效果。
处理这种情况的最佳方法是什么?我在哪里实际实现轮换的“之前”和“之后”状态?
我希望它能在 iOS 4+ 上运行。
Swift 3Said Ali Samed 的回答版本(willRotateToInterfaceOrientation
and didRotateFromInterfaceOrientation
已弃用):
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { context in
// Save the visible row position
self.visibleRows = self.tableView.indexPathsForVisibleRows!
context.viewController(forKey: UITransitionContextViewControllerKey.from)
}, completion: { context in
// Scroll to the saved position prior to screen rotate
self.tableView.scrollToRow(at: self.visibleRows[0], at: .top, animated: false)
})
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)