【问题标题】:Resize an inset UITextView when device rotates设备旋转时调整插入 UITextView 的大小
【发布时间】: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


【解决方案1】:

问题不在于我的 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
    }
}

【讨论】:

    猜你喜欢
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 2011-12-29
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多