我正试图设计我的图像和文字。我能够将图像置于顶部中心。但我不能让文字在图像下面。我已经包含了我的代码。我试图以编程方式进行约束。
class LadyDetailViewController: UIViewController {
var webView: WKWebView!
var detailItem: [String: String]!
override func loadView() {
webView = WKWebView()
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
//add image
let image:UIImage = UIImage(named: detailItem["image"]!)!
let imageView = UIImageView(image: image)
self.view.addSubview(imageView)
//image constraints
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.topAnchor,constant: 108).isActive = true
//share information with others
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
guard detailItem != nil else {return}
let name = detailItem["name"]
if let body = detailItem["body"] {
var html = "<html>"
html += "<head>"
html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
html += "<style> body { font-size: 150%; font-family: \"Palatino Linotype\", \"Book Antiqua\", Palatino, serif;} </style>"
html += "</head>"
html += "<body bgcolor=\"#eeebda\">"
html += "<h2><font color = \"#001547\">"
html += name!
html += "</font> </h2>"
html += "<font color = \"#140911\">"
html += body
html += "</font>"
html += "</body>"
html += "</html>"
webView.loadHTMLString(html, baseURL: nil)
}
}
@objc func shareTapped() {
let vc = UIActivityViewController(activityItems: [detailItem["name"]!,detailItem["body"]!], applicationActivities: [])
vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true)
}
}