ScrollView in NIB File Swift - ios

I'm trying to create a nib file where the view outlet is a uiview with a fullscreen UIScrollView. I want to add the actual view into the scrollview, if that makes sense (I will attach screen shots). I've tried a few different approaches, most being in Objective-C, but can't get this approach working. When i run the code below I get (lldb) in the console with no other error text.
class ScrollViewVC: UIViewController {
#IBOutlet weak var mainScrollView: UIScrollView!
#IBOutlet var contentView: UIView!
#IBOutlet weak var contentViewTitle: UILabel!
#IBOutlet weak var contentViewPrice: UILabel!
#IBOutlet weak var contentViewImageContainer: UIScrollView!
#IBOutlet var imageContainer: UIView!
#IBOutlet weak var imageOne: UIImageView!
#IBOutlet weak var imageTwo: UIImageView!
#IBOutlet weak var imageThree: UIImageView!
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
convenience init() {
self.init(nibName: "ScrollViewVC", bundle: nil)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.mainScrollView.addSubview(contentView)
self.mainScrollView.contentSize = self.contentView.frame.size
self.contentViewImageContainer.addSubview(self.imageContainer)
self.contentViewImageContainer.contentSize = self.imageContainer.frame.size
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

self.mainScrollView.addSubview(contentView)
this line crashes your app. Check where your contentView outlet points. It points to the view which in your IB has a name "Main Scroll View". Inside if it you have a scroll view to which you have a mainScrollView outlet connection.
So, you are trying to add to the scroll view the view which contains this scroll view. No wonder your app crashes.
Be very careful with the outlets which you assign.

Related

Make ViewController become a listener of a button clicked, located inside a custom view which located inside a TableViewCell xib?

I have a ViewController which holds a UITableView.
I created a UITableViewCell class named - CustomTableViewCell and connected it to a xib file with the same name. Inside this xib file I have a custom view named - CustomTextFieldView, and this custom view has a button in it, named - listButton.
My goal is that the ViewController will be a listener of the listButtonPressed action so that later help me to expand and collapse the table view cell every time this button pressed.
This is what i have tried
But I got exception when the relevant button clicked
Thanks in adavanced
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var customView: CustomTextFieldView!
#IBOutlet weak var label: UILabel!
static let identifier = "CustomTableViewCell"
static func nib() -> UINib {
return UINib(nibName: "CustomTableViewCell", bundle: nil)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func configure(withCell cell : PTTableModel) {
self.customView.initView(labelText: cell.label, placeHolder: cell.label, type: .name, hasList: cell.hasList)
}
}
protocol CustomTextFieldDelegate : class {
func didPressButton(button: UIButton)
}
class CustomTextFieldView: UIView {
#IBOutlet weak var listButton: UIButton!
#IBOutlet weak var bottomLine: UIView!
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var topLabel: UILabel!
#IBOutlet var contenView: UIView!
var delegate : CustomTextFieldDelegate!
static let identifier = "CustomTextFieldView"
static func nib() -> UINib {
return UINib(nibName: "CustomTextFieldView", bundle: nil)
}
override init(frame: CGRect) {
super.init(frame : frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
#IBAction func listButtonClicked(_ sender: Any) {
delegate.didPressButton(button: listButton)
}
}
class PetProfileDeatilsViewController: BaseUiViewController, UITableViewDelegate, UITableViewDataSource, CustomTextFieldDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var nextButton: UIButton!
private var cells = ViewModel.createTableSction()
override func viewDidLoad() {
super.viewDidLoad()
headerView.initView(mainLabel: "Create", subLabel: "a profile")
let vc = CustomTextFieldView()
vc.delegate = self
self.view.addSubview(vc)
tableView.register(CustomTableViewCell.nib(), forCellReuseIdentifier: CustomTableViewCell.identifier)
}
func didPressButton(button: UIButton) {
print("worked")
}

Nib awakefromNib() crashes

I have a class with a label and a collectionView, here is my code.
class CardSensors: UIView {
#IBOutlet weak var botName: UILabel!
#IBOutlet weak var sensorsCollectionView: UICollectionView!
var viewModel: NewsFeedViewModel! {
didSet {
setUpView()
}
}
override func awakeFromNib() {
super.awakeFromNib()
let nibName = UINib(nibName: "SensorCollectionViewCell", bundle:nil)
sensorsCollectionView.register(nibName, forCellWithReuseIdentifier: "SensorCollectionViewCell")
// Initialization code
}
func setUpView() {
botName.text = viewModel.botName
}
}
Obviously my cell has the correct Id and the correct reuse identifier.
And when I try to call the nib like this.
let sensorView = CardSensors()
sensorView.awakeFromNib()
I get Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value. I tried to force the values but I always get some errors.
What am I doing wrong?
As others have mentioned you should not call awakeFromNib() manually, it is something similar to how viewDidLoad() and viewWillAppear() get called automatically, the OS will fire it to let you know that your view has been loaded.
To initialize your view you could use the following static function, that allows you to call on the class to create a new instance of your CardSensors.
class CardSensors: UIView {
#IBOutlet weak var botName: UILabel!
#IBOutlet weak var sensorsCollectionView: UICollectionView!
var viewModel: NewsFeedViewModel! {
didSet {
setUpView()
}
}
static func loadFromNib() -> CardSensors {
return Bundle.main.loadNibNamed("CardSensors", owner: nil, options: nil)?.first as! CardSensors
}
override func awakeFromNib() {
super.awakeFromNib()
sensorsCollectionView.register(nibName, forCellWithReuseIdentifier: "SensorCollectionViewCell")
}
func setUpView() {
botName.text = viewModel.botName
}
}
let sensorView = CardSensors.loadFromNib()
You shouldn't call awakefromNib by yourself because awakefromNib will be called after you init your view by using UINib or load Nib from Bundle.main.loadNibNamed.
You can fix your error by using this:
class CardSensors: UIView {
#IBOutlet weak var botName: UILabel!
#IBOutlet weak var sensorsCollectionView: UICollectionView!
var viewModel: NewsFeedViewModel! {
didSet {
setUpView()
}
}
override init CardSensors() {
super.init()
let nibName = UINib(nibName: "SensorCollectionViewCell", bundle:nil)
sensorsCollectionView.register(nibName, forCellWithReuseIdentifier: "SensorCollectionViewCell")
// Initialization code
}
func setUpView() {
botName.text = viewModel.botName
}
}

How to change height of UITextview in prototype cell when return key tapped?

I am working on an iOS application in which I am using a prototype cell and in this cell I am using a UITextView.
I am using auto-layout with storyboard and set constraints so that height of
cell should automatically change.
I want the UITextView to resize the height so the text I'm typing fits inside it rather than you having to scroll to see the text that overflows. And tableview cell size should also be changed.
This is my code:
#IBOutlet weak var tableviewCart: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableviewCart.estimatedRowHeight = 142.0
self.tableviewCart.rowHeight = UITableViewAutomaticDimension
// Do any additional setup after loading the view.
}
class CartMenuItemCell:UITableViewCell{
#IBOutlet weak var heightConstraintsFoodInstruction: NSLayoutConstraint!
#IBOutlet var dishImageHeightConstraints: NSLayoutConstraint!
#IBOutlet weak var dishImageWidthConstraints: NSLayoutConstraint!
#IBOutlet weak var imgDishImageview: UIImageView!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var bottomLabelHeightConstraints: NSLayoutConstraint!
#IBOutlet weak var addMenuItemView: UIView!
#IBOutlet weak var addMenuItemButton: UIButton!
#IBOutlet weak var menuItemCountLabel: UILabel!
#IBOutlet weak var removeMenuItemButton: UIButton!
}
Your tableView configuration looks good.
#IBOutlet weak var tableviewCart: UITableView! {
didSet {
tableviewCart.estimatedRowHeight = 142.0
tableviewCart.rowHeight = UITableViewAutomaticDimension
}
}
You just need to add a height constraint to textView named textViewHeightConstraint and set it equal to textView 'textView.contentSize.height'
textViewHeightConstraint.constant = textView.contentSize.height
And layout its superview
textView?.superview?.layoutIfNeeded()

Swift: Help on Extensions

class quizController: UIViewController {
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
#IBOutlet weak var button4: UIButton!
var question : String
var options : [String]
var correctAns : Int
init() {
question = "What quiz are you taking?"
options = ["Medical", "Bollywood", "Math", "Trivia"]
correctAns = 0
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension CollectionType where Index == Int{
func shuffle() -> [Generator.Element] {
var readyToAskQuestions : [quizController] {
var questions = Array(question)
questions.count = questionNum
questions.shuffleInPlace()
return questions
}
convenience init() {
}
}
In the var questions line, I'm getting an error saying that
'question' is an unresolved identifier
I know that the function isn't in the quizController class and that's why, but I need that extension so that I can shuffle my quiz questions.
How should I approach this? Also, I'm gonna load the quiz questions from a json file and that's why there's a convenience init method there, but I'm getting an error stating that
initializers can only be declared within a type..
so that means it needs to be within a class?
Thanks for the help.
Try this instead of your code
var question = String()
var options = [String]()
var correctAns = Int()

Center words in Label which is in collection view

I want to center the text of a label which is in a collection view. I was looking inside the storyboard label settings and couldn't find a way to do it. My code is
class SingleRowCell: UICollectionViewCell {
#IBOutlet weak var tImageView: PFImageView!
#IBOutlet weak var tLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}}
Is this done by code or from the storyboard? When I build the app the words in the label come from the left and want it to be aligned to the center.
Override layoutSubviews()
class SingleRowCell: UICollectionViewCell {
#IBOutlet weak var tImageView: PFImageView!
#IBOutlet weak var tLabel: UILabel!
override func layoutSubviews() {
super.layoutSubviews()
tLabel.textAlignment = NSTextAlignment.Center
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}

Resources