disclaimer: I've found a number of these questions but none of them helped.
Just like the title says, the storyboard UI elements for my CarDetailViewController won't appear in the simulator, but they do in other view controllers in the project.
code:
import UIKit
import EventKit
import CoreData
class CarDetailViewController: UIViewController,
UIImagePickerControllerDelegate, UINavigationControllerDelegate,
NSFetchedResultsControllerDelegate {
/* -------------------------------------------------------------------------------------------------------- */
let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
/* -------------------------------------------------------------------------------------------------------- */
@IBOutlet weak var myImageView: UIImageView!
let picker = UIImagePickerController()
@IBOutlet weak var carLabel: UILabel!
@IBAction func photoFromLibrary(_ sender: UIButton) {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
self.view = UIImageView(image: UIImage(named: "Landing.png"))
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@nonobjc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject])
{
let chosenImage = info[UIImagePickerControllerOriginalImage]
myImageView.contentMode = .scaleAspectFit //3
myImageView.image = chosenImage as? UIImage //4
dismiss(animated: true, completion: nil) //5
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
{
dismiss(animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Try this :
Remove this Line :
self.view = UIImageView(image: UIImage(named: "Landing.png"))
You need to set background image as
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"Landing.png")!)
you donot assign an imageview into a uiview so instead of
self.view = UIImageView(image: UIImage(named: "Landing.png"))
You have the following options:
IMHO use the first approach, as the second might cause resolution issues with different devices
Edit update: Instead of initializing the vc as following
let destination = CarDetailViewController() // Your destination
try doing the following
let storyboard = UIStoryboard(name: <Storyboard-name>, bundle: nil)
let inquireViewController = storyboard.instantiateViewController(withIdentifier: String(describing:CarDetailViewController.self)) as! CarDetailViewController
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.