ios - 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)
}
}
}
*************************编辑结束 ************************* ********************
解决方案
在考虑之后,我通过将值传递给 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!
}
推荐阅读
- javascript - 检测异步函数的“仅返回承诺”状态
- button - 如何确定使用 xPath 选择了哪个选项按钮
- javascript - 为什么我无法访问设置了 src 或 srcdoc 属性的 iframe 文档?
- c# - 我怎么写这个,所以程序在用户输入他的名字后重新启动
- arrays - 在 Hive SQL 中分解列表以识别空白
- html - HTML CSS & Javascript 响应式导航栏
- solrj - 使用 Spring Data Solr 标记字段
- r - 我只能在 R 中安装软件包,只能从桌面打开 R
- java - 使用 Apache Commons CSV 从带有 Header 的 HashMap 列表中写入 CSV
- javascript - Protractor 的 getText() 方法不包括在 ::first-letter 伪选择器中完成的文本转换