我试图获取一个电子邮件地址,从中创建一个按钮,然后将该按钮添加到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)
}
}
}
大部分代码是从我正在尝试为作业修改的课堂演示中复制的。
答案 0 :(得分:0)
您需要在ViewController的视图类中将新按钮添加为子视图 使其出现在用户界面上。
func addNewButton(title: String)
{
let butt: UIButton = UIButton()
..
..
self.view.addSubview(butt)
}
答案 1 :(得分:0)
好的,根据这段代码,我从一开始就看到了几个问题。
您的addNewButton
函数创建一个按钮,但不设置框架或约束,它仅将该按钮添加到数组中。
首先我在想另一个答案是什么,您没有将按钮添加到视图中,但是后来我看到您确实在refreshList
函数上将按钮添加到了视图中。
在该函数内部,您实际上创建了一个按钮框架,但是在添加到视图之前,您从未将该框架分配给按钮。
因此,您的情况下的第一个测试就在scrollView.addSubview(button)
添加之前:
button.frame = buttonFrame
,看看是否可行