Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whole Cell is covered animation(Programmatically Solved!!) #283

Closed
oguzveozturk opened this issue Mar 31, 2020 · 3 comments
Closed

Whole Cell is covered animation(Programmatically Solved!!) #283

oguzveozturk opened this issue Mar 31, 2020 · 3 comments

Comments

@oguzveozturk
Copy link

⚠️ Please fill out this template when filing an issue.

🙏🏼 Please check if it already exists other issue related with yours.

What did you do?

Ekran-Resmi-2020-04-01-00-22-24
`import UIKit
import SkeletonView

class ViewController: UITableViewController, SkeletonTableViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()
    view.isSkeletonable = true
    tableView.isSkeletonable = true
    tableView.register(TableViewCell.self, forCellReuseIdentifier: "cell")
    tableView.estimatedRowHeight = 130
  tableView.rowHeight = UITableView.automaticDimension
    view.showAnimatedSkeleton()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    view.updateSkeleton()
}

override func updateViewConstraints() {
    super.updateViewConstraints()
    view.updateSkeleton()
}

func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 30
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 30
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
    cell.label.text = "test skeletonView"
    return cell
}

func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
    return "cell"
}

}

class TableViewCell: UITableViewCell {

lazy var label: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = ""
    label.isSkeletonable = true
    return label
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    isSkeletonable = true
    setupLayout()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupLayout() {
    addSubview(label)
    NSLayoutConstraint.activate([
        label.centerXAnchor.constraint(equalTo: self.centerXAnchor),
        label.centerYAnchor.constraint(equalTo: self.centerYAnchor),
        label.widthAnchor.constraint(equalToConstant: 300),
        label.heightAnchor.constraint(equalToConstant: 25)
    ])
}

}`

What did you expect to happen?

just want to try skeleton animation in a simple project

What happened instead?

as it shows in the picture; whole cell is covered with animation, not the label inside.

Steps to reproduce the behavior

my code is programmatically written. no storyboard or interface builder.
download project.

SkeletonView Environment

**SkeletonView version: 1.8.6
**Xcode version: 11.4
**Swift version: 5

@oguzveozturk oguzveozturk changed the title Whole Cell is covered animation Whole Cell is covered animation(Programmatically) Apr 1, 2020
@oguzveozturk oguzveozturk changed the title Whole Cell is covered animation(Programmatically) Whole Cell is covered animation(Programmatically Solved!!) Apr 1, 2020
@oguzveozturk
Copy link
Author

Solved issue;
when you writing programmatically; view objects should not be added to cell. Should be added to cell's contentView. (as it shows in the pic below)
@Juanpe
you must add this information to the "How to use" because there are lot of people who encounter the same problem while writing programmatically.

Ekran-Resmi-2020-04-01-17-45-26

@boungly
Copy link

boungly commented Sep 26, 2021

Hello @oguzveozturk

I also work on the UI programmatically, and in my case also on UITableViewCell, But I face a problem with the layout during animating as the layout keeps jumping.

UI Structure
image

OUTPUT

  • First Load
    First Load

  • Second ViewDidAppear
    First ViewDidAppear

  • Other ViewDidAppear
    Other ViewDidAppear

@mendesahdivio
Copy link

mendesahdivio commented Oct 6, 2023

hi i tried creating programmatic view as like mentioned above but i dont see any skeleton view for it

import UIKit
import SkeletonView

class ViewController: UIViewController {
private var viewModel = ViewModel()
var strings: [String]?

private let uiTableView: UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.estimatedRowHeight = 100
tableView.rowHeight = 100
tableView.isSkeletonable = true
return tableView
}()

override func viewDidLoad() {
super.viewDidLoad()
//set up the tableview
uiTableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "CellId")
setupTableView(uiTableView)
setTableViewDelegateAndDataSource(uiTableView)

//call the skeleton view
showAnimationForView()


// call network which is just a simple dispatchqeue with 5 seconds delay
callNetwork()

}

final func setupTableView(_ tableView: UITableView) {
view.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: self.view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
}

final func setTableViewDelegateAndDataSource(_ tableView: UITableView){
tableView.delegate = self
tableView.dataSource = self
}
}

//table view delegate and data source
extension ViewController: UITableViewDelegate, SkeletonTableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return strings?.count ?? 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellId", for: indexPath) as! CustomTableViewCell
if let value = strings?[indexPath.item] {
cell.labelView.text = value ?? "boyjdkjfhdsh"
cell.uiImageView.image = UIImage(systemName: "heart.fill")
}
return cell
}

func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
return "CellId"
}
}

//functions to call skeleton view animations
extension ViewController {

private final func showAnimationForView() {
view.showAnimatedSkeleton()
}

func stopSkeletonView() {
self.view.stopSkeletonAnimation()
self.view.hideSkeleton()
}

func callNetwork() {

DispatchQueue.main.asyncAfter(deadline: .now() + 5) {[weak self] in
  self?.strings =  self?.viewModel.getdata()
  self?.stopSkeletonView()
  self?.uiTableView.reloadData()
}

}
}

//dumy view model
struct ViewModel {
func getdata() -> [String] {
return ["hey", "there", "boy", "lol", "test", "make", "proud", "test", "you", "make it worth"]
}
}

//custom tableview cells with programatic UI
class CustomTableViewCell: UITableViewCell {

let labelView: UILabel = {
let labelView = UILabel()
labelView.font = .systemFont(ofSize: 25)
labelView.translatesAutoresizingMaskIntoConstraints = false
labelView.numberOfLines = 0
labelView.text = "some randon text"
labelView.isSkeletonable = true
return labelView
}()

let uiImageView: UIImageView = {
var imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage()
imageView.contentMode = .scaleAspectFit
imageView.frame.size.width = 70
imageView.frame.size.height = 70
imageView.clipsToBounds = true
imageView.isSkeletonable = true
return imageView
}()

private lazy var stackView: UIStackView = {
let hstack = UIStackView(arrangedSubviews: [
uiImageView,
labelView
])
hstack.translatesAutoresizingMaskIntoConstraints = false
hstack.axis = .horizontal
hstack.spacing = 10
hstack.distribution = .fillEqually
hstack.isSkeletonable = true
return hstack
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
isSkeletonable = true
contentView.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0),
stackView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0),
stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0),
stackView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0),
labelView.widthAnchor.constraint(equalToConstant: 200),
labelView.heightAnchor.constraint(equalToConstant: 25)
])

}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants