XCode Simulator black screen - swift

I Just start learning UIKit, i got a problem when trying to run the program, my simulator is going well, but simulator only shows black screen, without any problem on terminal, is there someone recognize my fault?
here is my PageViewController and screenshot blackscreen
import UIKit
class OnboardingViewController: UIPageViewController {
#IBOutlet weak var pageControllView: UIPageControl!
#IBOutlet weak var collectionView: UICollectionView!
var onboardingPage: [OnboardingModel] = []
override func viewDidLoad() {
super.viewDidLoad()
onboardingPage = [
OnboardingModel(description: "Welcome to Future Box", image: #imageLiteral(resourceName: "treasure")),
OnboardingModel(description: "Future Box will save your thoughts about about something and save it on your local data, you can set time for that", image: #imageLiteral(resourceName: "Idea")),
OnboardingModel(description: "And when you set the time for your thoughts, Future Box will remind you again when the time is up", image: #imageLiteral(resourceName: "clock"))
]
}
}
extension OnboardingViewController: UICollectionViewDelegate, UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return onboardingPage.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OnboardingCollectionViewCell.identifier, for: indexPath) as! OnboardingCollectionViewCell
cell.setupPage(page: onboardingPage[indexPath.row])
return cell
}
}

There are some possible reasons:
You set wrong Project -> Deployment Info -> Main Interface
Main Storyboard Base Name is wrong
Entry Point in the storyboard is not set
Not set:
collectionView.delegate = self
collectionView.dataSource = self
Usage of
class OnboardingViewController: UIPageViewController {}
instead of
class OnboardingViewController: UIViewController {}
You can check your view hierarchy from the visual debugger to see what is on device's screen.

Related

UICollectionView Cells overlapping with each other when i use a button to increase cell height

I have a CollectionView setup and i have a button inside that increases the cell height by a fixed amount. However when it does this is overlapps with the cell below meaning the cell below doesnt move down to make space.
Ive been trying to figure this out for days and i cant seem to get it working.
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var colors = [UIColor.red,UIColor.blue]
#IBOutlet weak var col: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! cellCollectionViewCell
cell.frame.size.height = CGFloat(317 + cell.button.tag*45)
cell.backgroundColor = colors[indexPath.row]
return cell
}
#IBAction func increaseHeight(_ sender: Any) {
col.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
class cellCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var butt: UIButton!
#IBAction func incHeight(_ sender: UIButton) {
sender.tag = sender.tag + 1
}
}
Editing the frame of the cell directly is probably not the best approach. Instead override collectionView sizeForItemAtIndexPath, and specify the height change there. This should allow the collection view to re-layout correctly with the new cell height.
You can see other questions like this one for more details

Swift: tvOS IBAction for UIControl in Collection View cell never gets called

I know this has been asked many times, but I am still learning, I have tried all possible solutions here in S.O. and I haven't had luck a single time. So this is the issue:
I am using Xcode 11 beta 3 (hopefully that ISN'T the issue!)
I have a simple collection view in a view controller
Inside the cell I have put a tvOS' TVPosterView - which inherits from UIControl and testing it by itself it does behave like a button (IBAction gets called).
On the collection view there is the animation when I push the poster
the poster view has a default image and I just change the title
I dragged an IBAction from the poster view to the cell class in IB
IBAction apart, the collection view shows and scrolls just fine, changing the titles of the posters
If I delegate the collection view to the view controller, collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) does get called.
Here is the code:
View Controller
import UIKit
import TVUIKit
class SecondViewController: UIViewController {
#IBOutlet weak var myView: UIView!
#IBOutlet weak var myCollection: MyCollection!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
extension SecondViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell
cell.myPoster.title! = "Header " + String(indexPath.row + 1)
cell.myPoster.tag = indexPath.row + 1
cell.posterTapAction = { cell in
print("Header is: \(cell.myPoster.title!)")
}
return cell
}
}
extension SecondViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// This one works - but that's not what I am looking for
print("From didSelectItemAt indexPath: \(indexPath.item + 1)")
}
}
Collection View
import UIKit
class MyCollection: UICollectionView {
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
Cell
import UIKit
import TVUIKit
class MyCell: UICollectionViewCell {
var posterTapAction: ((MyCell) -> Void)?
#IBOutlet weak var myPoster: TVPosterView!
#IBAction func myAction(_ sender: TVPosterView) {
posterTapAction?(self)
print("Poster pressed \(sender.tag)")
}
}
Any idea about what I am missing? I would just be happy to manage to print a dummy string after pressing a poster.
I have also tried the solutions with selectors and delegates, none worked. So, please let's concentrate on this particular solution with closures. Thanks!
I have finally found it. As usual it's due to tvOS focus. Basically the cell has to be not focusable, so that the UIControl element inside the cell (TVPosterView in my case, but it could be any UIControl) will be the focused one. It's tricking because graphically it does appear as if the poster had focus (one can rotate the poster a bit around).
The solution is to add collectionView(_:canFocusItemAt:) to the UICollectionViewDelegate
extension SecondViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView,
canFocusItemAt indexPath: IndexPath) -> Bool {
return false
}
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
// Now, thanks to the function above, this is disabled
print("From didSelectItemAt indexPath: \(indexPath.item + 1)")
}
}
I have tested it in the code of this question and added it also to my project, which finally works!

IBOutlet pageControl is invalid

I trying to add pageControl in viewController for display current page and total pages.
I have collectionView inside viewController. But if i add #IBOutlet in viewController i get the error:
"The pageControl outlet from the DetailViewController to the UIPageControl is invalid. Outlet cannot be connected to repeating content."
That means repeating content?
I understand that this question has been asked many times, but I tried what was suggested but nothing helped.
PageControl in main.storyboard have only one #IBOutlet and it throws an error...
If need my code, he is here:
class DetailViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDelegate, UITableViewDataSource {
// MARK: - IBOutlet's
#IBOutlet weak var collectionView: UICollectionView!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var pageControl: UIPageControl!
var hallImages: Hall?
var currentPage = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
tableView.estimatedRowHeight = 626
tableView.rowHeight = UITableViewAutomaticDimension
}
// MARK: - CollectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let imagesHall = hallImages?.ImagesHalls.count else {
return 0
}
return imagesHall
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! DetailCollectionViewCell
if let imagesHalls = hallImages?.ImagesHalls[indexPath.item] {
cell.imageView.sd_setImage(with: URL(string: imagesHalls))
}
return cell
}
}
How i can add pageControll in collectionView or collectionCell (i don't know how is better)?
I had the same problem. It is because your page control is a subview of the cell. You cannot do this.
take it out of your cell
put it in your main view/ safe area

Trouble added cell to UICollectionView

Trying to add a cell to my ViewController, and I'm having trouble. I'm wondering if I have all my delegates. Currently, I can change the color of the collection view's background, but can't add any cells.
I'm using an array of text from my Listing() object to drive the collection view cells. On viewDidLoad the length is 4. I envisioned using this count for the numberofItemsInSection, however, if I put the print statement there it is never printed. Which makes me think the wrong delegate is called and the method is never hit. Correct?
import UIKit
class ListingDetailViewController: UIViewController,
UICollectionViewDelegateFlowLayout {
var listing = Listing()
// #IBOutlet var image: UIImageView!
#IBOutlet var post: UITextView!
#IBOutlet var price: UILabel!
#IBOutlet var amenitiesCollection: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationItem.title = listing.title
self.amenitiesCollection!.backgroundColor = UIColor.gray
print(listing.amenities.count)//printed 4
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
print(listing.amenities.count)
return listing.amenities.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let identifier = "UICollectionViewCell"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
return cell
}
}
Make sure you have linked the CollectionView delegate and datasource to your view controller.
In viewDidLoad() add:
self.amenitiesCollection.delegate = self
self.amenitiesCollection.dataSource = self
==================================
EDIT:
If you have already linked the delegate and dataSource, try reloading the collection view in viewDidAppear(_:)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.amenitiesCollection.reloadData()
}

UICollectionView appears black when running the App

I am using swift 2.0 and xcode 7. I would like to have a vertical collection view with an array of image on my app. However when I run my project the UICollectionView area appears black. I have set the script on my ViewController but I don't what I'm missing. I already have set an identifier and all other stuff. Could somebody help me please. Thanks.
ViewController Script
ViewController error message
I guess you forgot about delegates, it's typical error.
Also, try use this example of code
class ExampleCollectionView: UIViewController, UICollectionViewDelegate
{
#IBOutlet weak var collectionView: UICollectionView!
let tshirtsArray = ["tshirt_1.png",
"tshirt_2.png",
"tshirt_3.png",
"tshirt_4.png"]
override func viewDidLoad()
{
super.viewDidLoad()
collectionView.delegate = self
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.tshirtsArray.count
}
func collectionView(cv: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: UICollectionViewCell = cv.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath)
let collectionImageView: UIImageView = cell.viewWithTag(100) as! UIImageView
collectionImageView.image = UIImage(named: self. tshirtsArray[indexPath.row])
return cell
}
}
I've made the UICollectionView display the images although i still need to update the sizes. I've followed #pdobsk advice to look through the error message and it says that i should consider the cell size together with the margin and other stuffs because the cell size is the same size with the collection view.
error fixed!