我有一个名为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
)
}
问题是当屏幕旋转时,inset 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
)
}
}
不幸的是,这对景观到纵向的过渡没有帮助,并且让我有一堆界限可以调整。有什么建议吗?
答案 0 :(得分:0)
问题不在于我的UITextView或其插图的大小调整,我发现,通过一些打印语句,它按预期和自动工作。相反,问题是我为模糊边缘创建的遮罩层没有调整大小。以下(使用insetBy(dx:dy :)更改UITextView的边界而不是其框架)是我更正的代码:
/**
- 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 textViewInset = detailView.bounds.width * 0.1
detailTextView.bounds = detailView.bounds.insetBy(
dx: textViewInset,
dy: textViewInset
)
// Set up text view attributes.
detailTextView.allowsEditingTextAttributes = true
let textContainerInset = detailTextView.bounds.width * 0.05
detailTextView.textContainerInset = UIEdgeInsetsMake(
textContainerInset,
textContainerInset,
textContainerInset,
textContainerInset
)
// 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
)
}
/**
- 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 viewWillTransition(
to size: CGSize,
with coordinator: UIViewControllerTransitionCoordinator)
{
print("viewWillTransition")
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil) {
[unowned self] _ in
// Blur edges of text view (see Joy, 2016).
let maskLayer = CAGradientLayer()
maskLayer.frame = self.detailTextView.bounds
maskLayer.shadowRadius = 5
maskLayer.shadowPath = CGPath(
roundedRect: self.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
self.detailTextView.layer.mask = maskLayer
}
}