【发布时间】:2018-05-02 13:59:44
【问题描述】:
我有一个名为 detailView 的 UIView,它有两个子视图,一个名为 detailTextView 的 UITextView 和一个名为 detailImageView 的 UIImageView,它们是彼此的兄弟。 detailView 包含在拆分视图的默认详细信息 UIView 中。我正在使用自动调整大小在我的主情节提要中设置所有这些。从视觉上看,层次结构如下:
Top Layout Guide
Bottom Layout Guide
View
- Detail View
- - Detail Text View
- - Detail Image View
Navigation Item
detailTextView 从每边插入 10%,边缘模糊,当视图出现时在图像顶部交叉溶解,如下:
/**
- reference:
Joy, V. (2016, December 15). Swift 3: How to create blurred side edges/
corners of a UIImageView?. Message posted to
https://stackoverflow.com/a/41158594/6084947
*/
internal override func viewDidAppear(_ animated: Bool) {
print("detailViewDidAppear")
super.viewDidAppear(animated)
// Resize the text view.
let inset = detailView.bounds.width * 0.2
detailTextView.frame = CGRect(
x: inset / 2,
y: inset / 2,
width: detailView.bounds.width - inset,
height: detailView.bounds.height - inset
)
// Set up text view attributes.
detailTextView.allowsEditingTextAttributes = true
let xInset = detailTextView.bounds.width * 0.05
let yInset = detailTextView.bounds.height * 0.05
detailTextView.textContainerInset = UIEdgeInsetsMake(
yInset,
xInset,
yInset,
xInset
)
// Blur edges of text view (see Joy, 2016).
let maskLayer = CAGradientLayer()
maskLayer.frame = detailTextView.bounds
maskLayer.shadowRadius = 5
maskLayer.shadowPath = CGPath(
roundedRect: detailTextView.bounds.insetBy(dx: 5, dy: 5),
cornerWidth: 10,
cornerHeight: 10,
transform: nil
)
maskLayer.shadowOpacity = 1
maskLayer.shadowOffset = CGSize.zero
maskLayer.shadowColor = UIColor.white.cgColor
detailTextView.layer.mask = maskLayer
// Fade in text view.
UIView.transition(
with: detailView,
duration: 3,
options: .transitionCrossDissolve,
animations:
{ self.detailView.bringSubview(toFront: self.detailTextView) },
completion: nil
)
}
问题是屏幕旋转时插入的 detailTextView 无法正确调整大小。从横向模式加载详细视图时,在 iPad 模拟器上旋转到纵向模式时,它不会填满超级视图。
我尝试了以下方法:
internal override func viewWillTransition(
to size: CGSize,
with coordinator: UIViewControllerTransitionCoordinator)
{
print("viewWillTransition")
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil) {
[unowned self] _ in
// Resize text view.
let inset = size.width * 0.2
self.detailTextView.frame = CGRect(
x: inset / 2,
y: inset / 2,
width: size.width - inset,
height: size.height - inset
)
}
}
不幸的是,这无助于横向到纵向的过渡,并且给我留下了很多需要调整的界限。有什么建议吗?
【问题讨论】:
-
iPad 的尺寸等级是相同的,除非在某种类型的分屏模式下。不要使用
size。相反 - 你确实有正确的覆盖 - 使用主视图的bounds。 (换句话说,你的事情是正确的 IMO。) -
谢谢,但用
self.view.bounds替换size并没有帮助。将横向旋转为纵向时,文本视图仍然不会放大以填满屏幕的下半部分。 -
顺便说一句,在 iPad 上处于横向模式时,拆分视图的主视图和细节都可见。
-
我认为答案在 Stanford - Developing iOS 9 Apps with Swift - 4. Views 的 13:45。如果这确实是解决方案,我会尽快回答我自己的问题。
标签: ios swift uiview uitextview xcode9