Я пытаюсь получить адрес электронной почты, создать из него кнопку, а затем добавить эту кнопку в 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)
}
}
}
Большая часть этого кодаскопировано с демонстрации в классе, которую я пытаюсь изменить для задания.