我正在从事ios项目。我想调整文本视图'UIEEdgeInsets的内部Margin值。我尝试使用'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而
隐式展开一个Optional值

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

*************************编辑第二个*********************** ********************

ModalViewController

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)
        }
    }


}

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

最佳答案

考虑一下之后,我通过将值传递给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)

        }
    }

ModalViewController

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

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

08-19 12:27