gpt4 book ai didi

ios - UIButtons 未显示在 UIScrollView 中

转载 作者:行者123 更新时间:2023-11-29 05:58:12 25 4
gpt4 key购买 nike

我正在尝试获取一个电子邮件地址,从中创建一个按钮,然后将该按钮添加到 UIScrollView 中。

问题是按钮在 UIScrollView 中不可见。电子邮件正在添加到列表中,按钮正在创建但不可见。

代码:

import UIKit

let BUTTON_SPACING = 10
let BUTTON_HEIGHT = 40

class ViewController: UIViewController {

@IBOutlet weak var mailField: UITextField!
@IBOutlet weak var scrollView: UIScrollView!

var fileUrl = NSURL()
var emails = [String]()

var mailButtArr: [UIButton] = []
var deletButtArr: [UIButton] = []

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
print(documentsURL)
fileUrl = documentsURL.appendingPathComponent("emails.plist") as NSURL

let fileManager = FileManager.default
if(!fileManager.fileExists(atPath: fileUrl.path!))
{
print("email.plist is not found with filemanager")
NSKeyedArchiver.archiveRootObject(emails, toFile: fileUrl.path!)
}
else
{
emails = NSKeyedUnarchiver.unarchiveObject(withFile: fileUrl.path!) as! [String]
print(emails.count, " emails found ")
for s in emails
{
print(s)
addNewButton(title: s)
}
}
mailField.text = "xyz@mail.com"
}

@IBAction func AddEmail(sender: AnyObject)
{
mailField.resignFirstResponder()

let email: String = mailField.text!

emails.append(email)
mailField.text = "xyz@mail.com"

NSKeyedArchiver.archiveRootObject(emails, toFile: fileUrl.path!)
print("saved")

addNewButton(title: email)
}

@objc func buttonTouched(sender: UIButton)
{
let urlString: String = sender.titleLabel!.text!
let url: NSURL = NSURL(string: urlString)!
UIApplication.shared.openURL(url as URL)
}

func addNewButton(title: String)
{
let butt: UIButton = UIButton()
butt.setTitle(title, for: .normal)

butt.backgroundColor = UIColor.green
butt.layer.borderColor = UIColor.black.cgColor
butt.layer.borderWidth = 0.5
butt.layer.cornerRadius = 10.0

butt.addTarget(self, action: #selector(ViewController.buttonTouched(sender:)), for: .touchUpInside)
mailButtArr.append(butt)
mailButtArr = mailButtArr.sorted {$0.titleLabel!.text! < $1.titleLabel!.text!}

let newHeight: Float = Float((mailButtArr.count) * (BUTTON_HEIGHT + BUTTON_SPACING)) + Float(BUTTON_SPACING)
let contentSize: CGSize = CGSize(width: scrollView.frame.size.width, height: CGFloat(newHeight))

scrollView.contentSize = contentSize

refreshList()
}

func refreshList()
{
let subviews = scrollView.subviews as [UIView]
for v in subviews
{
if let button = v as? UIButton
{
button.removeFromSuperview()
}
}
deletButtArr.removeAll()

var buttonOffset: CGFloat = CGFloat(BUTTON_SPACING)

for button: UIButton in mailButtArr
{
var buttonFrame: CGRect = button.frame
buttonFrame.origin.x = CGFloat(BUTTON_SPACING)
buttonFrame.origin.y = buttonOffset

buttonFrame.size.width = 200
buttonFrame.size.height = CGFloat(BUTTON_HEIGHT)
print("X:", BUTTON_SPACING, " Y:", buttonOffset, " W: ", buttonFrame.size.width, " H: ", BUTTON_HEIGHT)
scrollView.addSubview(button)

/* let delButt: UIButton = UIButton(type: .detailDisclosure)
deletButtArr.append(delButt)
buttonFrame = delButt.frame
buttonFrame.origin.x = 340
buttonFrame.origin.y = buttonOffset + 10
delButt.frame = buttonFrame

scrollView.addSubview(delButt)*/

buttonOffset += CGFloat(BUTTON_HEIGHT + BUTTON_SPACING)
}
}

}

此代码的大部分内容是从类演示中复制的,我正在尝试针对作业进行修改。

最佳答案

好吧,根据这段代码,我从一开始就发现了一些问题。

您的 addNewButton 函数创建一个按钮,但不设置框架或约束,并且仅将该按钮添加到数组中。

首先,我在想另一个答案所说的,您没有将按钮添加到 View 中,但后来我看到您确实在 refreshList 函数上将按钮添加到了 View 中。

在该函数中,您实际上创建了一个按钮框架,但在添加到 View 之前从未将该框架分配给按钮。

因此,您的情况下的第一个测试是在 scrollView.addSubview(button) 添加之前:button.frame = buttonFrame 看看是否有效

关于ios - UIButtons 未显示在 UIScrollView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54935490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com