首页 > 解决方案 > UIEdgeInsets.init 不适用于 Xcode 中的 Swift

问题描述

我正在做ios项目。我想调整文本视图“UIEEdgeInsets”的内部边距值。我尝试使用“init”。但它显示一个错误。

使用未解析的标识符“底部”

参考了官方文档的使用,没有发现问题。我错过了什么?

用法

    @IBAction func NextButtonfuc(_ sender: Any) {
        if  agreeOneCheck.isSelected != true ||
            agreeThreeCheck.isSelected != true ||
            allAgreeCheck.isSelected != true ||
            agreeTwoCheck.isSelected != true
        {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
            myAlert.modalPresentationStyle = .overCurrentContext
            myAlert.modalTransitionStyle = .crossDissolve
            myAlert.modalCustomAlert.textContainerInset = UIEdgeInsets.init(top: 100.0, left: 0.0, bottom: 0.0, right: 0.0) // get Error
            myAlert.text = "OK Thanks"
            self.present(myAlert, animated: false, completion: nil)

        }
    }

*************************编辑开始 *********************** ********************

我通过参考答案更正了代码。

    @IBAction func NextButtonfuc(_ sender: Any) {
        if  agreeOneCheck.isSelected != true ||
            agreeThreeCheck.isSelected != true ||
            allAgreeCheck.isSelected != true ||
            agreeTwoCheck.isSelected != true
        {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
            myAlert.modalPresentationStyle = .overCurrentContext
            myAlert.modalTransitionStyle = .crossDissolve
            let insets = UIEdgeInsets.init(top: 100.0, left: 0.0, bottom: 0.0, right: 0.0)
            myAlert.modalCustomAlert.textContainerInset = insets //  Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
            myAlert.text = "OK Thanks"
            self.present(myAlert, animated: false, completion: nil)

        }
    }

获取错误:线程 1:致命错误:在隐式展开可选值时意外发现 nil

*************************编辑结束 ************************* ********************

************************* 编辑第二次 *********************** ********************

模态视图控制器

import Foundation
import UIKit

class ModalViewController : UIViewController {

    var text: String?


    @IBOutlet weak var modalCustomAlert: UITextView!
    @IBOutlet weak var okButton: UIButton!

    @IBAction func okPress(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        modalCustomAlert.layer.cornerRadius = 8
        self.modalCustomAlert.text = text
    }





    func changeViewFont() {
        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height

        if screenWidth < 375 {
            // iPhone 4inch and 3.5inch. Smaller than iPhone 8
            // Call change font
            modalCustomAlert.font = UIFont.systemFont(ofSize: 12)

            okButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)

        }
        if screenHeight > 667 {

            modalCustomAlert.font = UIFont.systemFont(ofSize: 16)

            okButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        }
    }


}

*************************编辑结束 ************************* ********************

标签: ioscssswifttextview

解决方案


在考虑之后,我通过将值传递给 Controller 类解决了这个问题。

    @IBAction func NextButtonfuc(_ sender: Any) {
        if  agreeOneCheck.isSelected != true ||
            agreeThreeCheck.isSelected != true ||
            allAgreeCheck.isSelected != true ||
            agreeTwoCheck.isSelected != true
        {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
            myAlert.modalPresentationStyle = .overCurrentContext
            myAlert.modalTransitionStyle = .crossDissolve
            let insets = UIEdgeInsets.init(top: 100.0, left: 0.0, bottom: 0.0, right: 0.0)
            myAlert.marginValue = insets 
            myAlert.text = "OK Thanks"
            self.present(myAlert, animated: false, completion: nil)

        }
    }

模态视图控制器

    var text: String?
    var marginValue : UIEdgeInsets?
...

    override func viewDidLoad() {
        super.viewDidLoad()
        modalCustomAlert.layer.cornerRadius = 8
        self.modalCustomAlert.text = text
        self.modalCustomAlert.textContainerInset = marginValue!
    }

推荐阅读