ios - UIEdgeInsets.init 不适用于 Xcode 中的 Swift

标签 ios css swift textview

我正在做 ios 元素。我想调整 TextView “UIEEdgeInsets”的内部边距值。我尝试使用“init”。但它显示错误。

Use of unresolved identifier 'bottom'

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

用法

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

        }
    }

get error : Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

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

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

模态视图 Controller

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)

        }
    }

模态视图 Controller

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

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

关于ios - UIEdgeInsets.init 不适用于 Xcode 中的 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57820316/

相关文章:

ios - 从 GameCenter 帐户检索用户所在国家或地区的最佳方法是什么?

iphone - 如何将 iTunes 音乐文件转换为低带宽 PCM 以供上传

ios - 为 UITableViewController 添加属性

javascript - Tic-Tac-Toe 自动播放无法按预期工作

ios - 跨 ViewController 设置和访问全局属性的正确方法是什么?

ios - 使用 iOS 6/7 Deltas 时 UIWebView 变得太大

html - 整个网站呈灰度,除了一些 div 不工作

html - CSS :after tag with background image and %

ios - 为什么我的 UIView 背景在切换到下一个 View 时会变形?

swift - 如何从 firebase 存储中获取 imageUrl 并存储在 Xcode 10 swift 4.2 中的 firebase 数据库中