The circle uiview is hidden when the cell is selected. How to fix it?
bringSubviewToFront not helping.
screenshot
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.selectedBackgroundView?.bringSubviewToFront(statusView)
}
Please help!
set your view's backgroundColor by override func setHighlighted(highlighted: Bool, animated: Bool)and func setSelected(selected: Bool, animated: Bool)like this:
class Cell: UITableViewCell {
var redView :UIView!
var yellowView :UIView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
redView = UIView()
redView.frame = CGRect(x: 0, y: 10, width: 100, height: 20)
self.contentView.addSubview(redView)
yellowView = UIView()
yellowView.frame = CGRect(x: 200, y: 10, width: 100, height: 20)
self.contentView.addSubview(yellowView)
redView.backgroundColor = UIColor.redColor()
yellowView.backgroundColor = UIColor.yellowColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
redView.backgroundColor = UIColor.redColor()
yellowView.backgroundColor = UIColor.yellowColor()
}
override func setHighlighted(highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
redView.backgroundColor = UIColor.redColor()
yellowView.backgroundColor = UIColor.yellowColor()
}
}
While selecting the cells UIView, all painted in the background of the cell, so the UIView is hidden. If repaint after selection, then everything will be.
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
switch status {
case "8","9":
// print("Cancel")
statusView.backgroundColor = FlatUIColors.thunderBirdColor()
case "10","11","12":
// print("Success")
statusView.backgroundColor = FlatUIColors.nephritisColor()
case "20","21","22","23":
// print("Wait")
statusView.backgroundColor = FlatUIColors.wetAsphaltColor()
case "30","31":
// print("Processing")
statusView.backgroundColor = FlatUIColors.peterRiverColor()
default:
break
}
}
}
Related
I have a custom navigationBar view like this.
And I use sizeThatFits seems not work.
How to achieve this?
class ListVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let height: CGFloat = 140
let bounds = self.navigationController?.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds?.width ?? UIScreen.main.bounds.width, height: height)
}
}
class CustomNavi: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.commonInit()
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 140)
}
private func commonInit() {
let bundle = Bundle.init(for: CustomNavi.self)
if let viewToAdd = bundle.loadNibNamed("CustomNavi", owner: self, options: nil), let contentView = viewToAdd.first as? UIView {
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
}
Encountered a problem: largeTitle of navigation bar collapsing to small title ~once per 20 attempts after refreshControl.endRefreshing().
tableView is first subview of subviews hierarchy, and top constraint is to superview (not to safeArea)
refreshControl added to tableView.refreshControl, not as subview of tableView
was trying call navigationBar.sizeToFit() after endRefreshing - not success.
Controller code:
override public func viewDidLoad() {
super.viewDidLoad()
definesPresentationContext = true
title = L10n.Trainers.trainers.uppercased()
navigationController?.navigationBar.layoutMargins = .init(top: 0, left: 16, bottom: 0, right: 16)
configureSubviews()
makeConstraints()
configureBindings()
configureActions()
setupTableView()
setupResultsTableView()
}
override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.sizeToFit()
}
override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
navigationController?.navigationItem.largeTitleDisplayMode = .automatic
}
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
vm.onViewDidAppear()
mainView.tableView.refreshControl = mainView.refreshControl
}
override public func loadView() {
view = mainView
}
Refresh-logic code:
vm.isLoading.drive { [weak self] loading in
guard let self = self else { return }
if self.mainView.refreshControl.isRefreshing {
if !loading {
self.mainView.tableView.refreshControl?.endRefreshing()
}
} else {
if loading {
var style = LoaderParameters.blue
style.installConstraints = false
self.mainView.tableView.startLoading(with: style)
self.mainView.tableView.existedLoaderView?.centerX().centerY(-40)
} else {
self.mainView.tableView.stopLoadingProgress()
}
}
}.store(in: &subscriptions)
View code:
final class TrainersView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
configureSubviews()
makeConstraints()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
private(set) lazy var refreshControl = UIRefreshControl().configureWithAutoLayout {
$0.tintColor = Asset.mainGrey.color
}
private(set) lazy var topView: UIView = {
let view = UIView()
view.backgroundColor = .clear
return view
}()
private(set) lazy var tableView = UITableView().configureWithAutoLayout {
$0.register(TrainerCell.self)
$0.tableFooterView = UIView()
$0.showsVerticalScrollIndicator = false
$0.separatorStyle = .none
$0.backgroundColor = Asset.mainWhite.color
}
private func configureSubviews() {
addSubview(tableView)
topView.addSubview(segmentedControl)
tableView.tableHeaderView = topView
tableView.tableHeaderView?.frame.size.height = 60
tableView.tableFooterView?.frame.size.height = 40
tableView.reloadData()
}
private func makeConstraints() {
tableView.pinToSuperview()
segmentedControl.centerX()
segmentedControl.bottomAnchor.constraint(equalTo: topView.bottomAnchor, constant: -8).priority(999).activate()
segmentedControl.widthAnchor ~ widthAnchor - 16 * 2
}
}
I have this table view but the separator line looks like double line
In the storyboard I have this settings (table view):
Style: Plain
Separator: None
This is my code:
class ListTableViewCell: UITableViewCell {
#IBOutlet weak var transactionList: ransactionList!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
let separator = UIView(frame: CGRect(x: 8, y: bounds.size.height - 0.5, width: bounds.size.width - 22, height: 1))
separator.backgroundColor = UIColor.blue
contentView.addSubview(separator)
}
}
I expected something like this:
Here is how I made my own seperator line in the UITableViewDataSourceDelegate method.
//Separator Full Line
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = .zero
cell.layoutMargins = .zero
I believe I also had
separatorStyle="default"
on the tableview
I have built my own UITableViewCell (entirely by code, no interface builder) and I'm getting the fallowing exception:
[...] NSInternalInconsistencyException', reason: 'Could not load NIB in
bundle: 'NSBundle [...]
Im fairly certain I don't need to use awakeFromNib() (see code) but this is the only way I got it working when passing it as "non-reusable"
// this works
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath [...] {
return myCustomCell()
// this fails
tableView.register(MyCustomCell.self, forCellReuseIdentifier: "foo")
//throws immediately after this line: "[...] Could not load NIB in bundle"
My custom cell:
class TextValueTableViewCell: UITableViewCell {
let textField: UITextField = UITextField(
frame: CGRect(x: 30, y: 5, width: 350, height: 25)
)
let label = UILabel(frame: CGRect(x: 30, y: 5, width: 350, height: 25))
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.awakeFromNib();
}
override func awakeFromNib() {
super.awakeFromNib()
self.label.text = "oh"
self.contentView.addSubview(self.label)
self.contentView.addSubview(self.textField)
self.textField.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -25).isActive = true
self.textField.translatesAutoresizingMaskIntoConstraints = false;
self.textField.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor, constant: 0).isActive = true
self.textField.widthAnchor.constraint(equalTo: self.label.widthAnchor, constant: 0).isActive = true
self.textField.textAlignment = .right;
self.textField.placeholder = "Account Name"
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
}
As I said, I have not used the interface builder (and I'm not planing on it) so I'm not sure why it would try to load something from the bundle. Specifically, wouldn't I have to register it as a NIB then?
tableView.register(<#T##nib: UINib?##UINib?#>, forCellReuseIdentifier: <#T##String#>)
class TextValueTableViewCell: UITableViewCell {
let textField: UITextField = UITextField(
frame: CGRect(x: 30, y: 5, width: 350, height: 25)
)
let label = UILabel(frame: CGRect(x: 30, y: 5, width: 350, height: 25))
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setupCell()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupCell()
}
func setupCell() {
label.text = "oh"
contentView.addSubview(label)
contentView.addSubview(textField)
textField.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -25).isActive = true
textField.translatesAutoresizingMaskIntoConstraints = false;
textField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
textField.widthAnchor.constraint(equalTo: label.widthAnchor, constant: 0).isActive = true
textField.textAlignment = .right;
textField.placeholder = "Account Name"
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
For example If your using a UIViewController class name as TextPicker
Then u need to use like Bellow Code
let bundle = Bundle(for: TextPicker.self)
super.init(nibName: "TextPicker" , bundle: bundle)
If You are using TableView with Xib in other project as Framework
tableView.register(UINib.init(nibName: "TextPickerCell", bundle: bundle), forCellReuseIdentifier: "TextPickerCell")
let bundle = Bundle(for: TextPicker.self)
I am completely lost. I have a toggle button(UISwitch) in one of my screens I have added a target to the switch to recognize changes in the switch. However when the switch is toggled nothing happens and I am confused and lost.
import Foundation
import UIKit
class PrivateCell: UITableViewCell {
var stackView: UIStackView?
let switchStatementLabel : UILabel = {
let switchStatementLabel = UILabel()
switchStatementLabel.textAlignment = .justified
switchStatementLabel.text = "Make Profile Private"
return switchStatementLabel
}()
let privateSwitch : UISwitch = {
let privateSwitch = UISwitch(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
privateSwitch.isOn = false
privateSwitch.onTintColor = UIColor.rgb(red: 44, green: 152, blue: 229)
privateSwitch.addTarget(self, action: #selector(switchToggled(_:)), for: UIControlEvents.valueChanged)
return privateSwitch
}()
#objc func switchToggled(_ sender: UISwitch) {
if privateSwitch.isOn {
print("switch turned off")
}else{
print("switch turned on")
}
}
#objc func setupViews(){
backgroundColor = .white
stackView = UIStackView(arrangedSubviews: [ switchStatementLabel, privateSwitch])
stackView?.axis = .horizontal
stackView?.distribution = .equalSpacing
// stackView?.spacing = 10.0
if let firstStackView = stackView{
self.addSubview(firstStackView)
firstStackView.snp.makeConstraints { (make) in
make.edges.equalTo(self).inset(10)
}
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I have added my code could anyone help me please
Try this
import Foundation
import UIKit
class PrivateCell: UITableViewCell {
var stackView: UIStackView?
let switchStatementLabel : UILabel = {
let switchStatementLabel = UILabel()
switchStatementLabel.textAlignment = .justified
switchStatementLabel.text = "Make Profile Private"
return switchStatementLabel
}()
let privateSwitch : UISwitch = {
let privateSwitch = UISwitch(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
privateSwitch.isOn = false
privateSwitch.onTintColor = UIColor.rgb(red: 44, green: 152, blue: 229)
return privateSwitch
}()
#objc func switchToggled(_ sender: UISwitch) {
if privateSwitch.isOn {
print("switch turned off")
}else{
print("switch turned on")
}
}
#objc func setupViews(){
privateSwitch.addTarget(self, action: #selector(switchToggled(_:)), for: UIControlEvents.valueChanged)
backgroundColor = .white
stackView = UIStackView(arrangedSubviews: [ switchStatementLabel, privateSwitch])
stackView?.axis = .horizontal
stackView?.distribution = .equalSpacing
// stackView?.spacing = 10.0
if let firstStackView = stackView{
self.addSubview(firstStackView)
firstStackView.snp.makeConstraints { (make) in
make.edges.equalTo(self).inset(10)
}
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
#IBOutlet var swtOnlineOfline: UISwitch!
swtOnlineOffline.addTarget(self, action: #selector(self.onlineOfflineSwitchValueChange(_:)), for: .valueChanged)
#IBAction func onlineOfflineSwitchValueChange(_ sender: UISwitch)
{
if sender.isOn
{
swtOnlineOffline.setOn(false, animated: true)
}
else
{
swtOnlineOffline.setOn(true, animated: true)
}
}