I have a inputView where a collectionView will be displayed. I used auto layout for that inputView. When running the app numberOfItemsInSection is called but cellForItemAt is not called. So no cell is displaying. Did I do anything wrong using constraint?
I've also created project https://drive.google.com/file/d/0B5UHWsK1E6dSRDA5bmtSY2ZZbGs/view
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
textField.inputView = InputPhotoView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 265))
textField.becomeFirstResponder()
}
InputPhotoView.swift
class InputPhotoView: UIView {
let CellIdentifier = "Cell"
override init(frame: CGRect) {
super.init(frame: frame)
collectionView.dataSource = self
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: CellIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("Not yet implented")
}
func setupViews() {
self.backgroundColor = UIColor.brown
self.addSubview(collectionView)
setupConstraints()
}
func setupConstraints() {
NSLayoutConstraint.activate([
collectionView.leadingAnchor.constraint(equalTo: self.trailingAnchor, constant: 50),
collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0),
collectionView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0),
collectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0)
])
}
public lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: 100, height: 100)
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.contentInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
return collectionView
}()
}
DataSource
extension InputPhotoView : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("number of items")
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier, for: indexPath) as! CustomCell
return cell
}
}
Your code has constraint conflicts so the collectionView is not visible. Try changing this line:
collectionView.leadingAnchor.constraint(equalTo: self.trailingAnchor, constant: 50)
To something like this:
collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 50)
Related
I would like to adjust collectionview header width in iPad.
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 668, height: 44.0)
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 50)
}
I explicitly set the width even though it's showing headerView base on collectionView width.
Current Behaviour:
Expected:
Thi is sample code to do it:
declare your collection view, register cell, register header and set constraints:
class ViewController: UIViewController {
var collectionview: UICollectionView!
var cellId = "Cell"
let headerId = "myFooterView"
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
var myUIViewArr = ["1", "2", "3", "4", "5", "6", "7"]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
layout.scrollDirection = .vertical
collectionview = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionview.dataSource = self
collectionview.delegate = self
collectionview.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
collectionview.showsVerticalScrollIndicator = false
collectionview.backgroundColor = .black
collectionview.register(MyHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId)
collectionview.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(collectionview)
collectionview.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
collectionview.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
collectionview.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
collectionview.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
}
}
now set collectionView delegate and datasource method:
extension ViewController : UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return myUIViewArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionview.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
cell.contentView.backgroundColor = .red
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
layout.scrollDirection = .vertical
let cellInRow = 3
let totalSpace = layout.sectionInset.left + layout.sectionInset.right + (layout.minimumInteritemSpacing * CGFloat(cellInRow - 1))
let mySize = collectionView.bounds.width - totalSpace
let size = mySize / CGFloat(cellInRow)
return CGSize(width: size, height: size)
}
//set header view height
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 44)
}
//set header view
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId, for: indexPath) as! MyHeaderView
return view
}
}
set header class:
class MyHeaderView: UICollectionReusableView {
let labelHeader = UILabel()
let imageV: UIImageView = {
let iv = UIImageView()
iv.image = UIImage(systemName: "arrow.right.circle.fill")
iv.contentMode = .scaleAspectFill
return iv
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
labelHeader.text = "Add Your Text here"
labelHeader.textColor = .black
labelHeader.numberOfLines = 0
labelHeader.backgroundColor = .white
labelHeader.translatesAutoresizingMaskIntoConstraints = false
imageV.tintColor = .orange
imageV.translatesAutoresizingMaskIntoConstraints = false
addSubview(imageV)
imageV.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
imageV.heightAnchor.constraint(equalToConstant: 30).isActive = true
imageV.widthAnchor.constraint(equalToConstant: 30).isActive = true
imageV.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
addSubview(labelHeader)
labelHeader.topAnchor.constraint(equalTo: topAnchor).isActive = true
labelHeader.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
labelHeader.trailingAnchor.constraint(equalTo: imageV.leadingAnchor).isActive = true
labelHeader.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and your cell class:
class MenuCell: UICollectionViewCell {
let containerView = UIView()
let myView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
myView.translatesAutoresizingMaskIntoConstraints = false
contentView.backgroundColor = .clear
containerView.backgroundColor = .clear
containerView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(containerView)
containerView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
containerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
containerView.addSubview(myView)
myView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true
myView.heightAnchor.constraint(equalToConstant: 60).isActive = true
myView.widthAnchor.constraint(equalToConstant: 60).isActive = true
myView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
This is the result:
if you want some padding on the left of your header label in MyHeaderView simply add leading constraint constant like this:
labelHeader.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
and this is the result:
I have a UICollectionView inside the UINavigationItem's title view acting as a menu. I am unable to select items within the collection view. If I place the UICollectionView outside of the navigation item and into my view controller, everything works as intended.
UIViewController
let menuView = HomeMenuView()
override func viewDidLoad() {
navigationItem.titleView = menuView
menuView.delegate = self
}
HomeMenuView (UICollectionView)
class HomeMenuView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var sections = ["Recent", "Following"]
var delegate: HomeMenuDelegate!
var selectedIndex = 0
var collectionView: UICollectionView!
override init(frame: CGRect) {
super.init(frame: frame)
create()
}
func create() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
addSubview(collectionView)
collectionView.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: UIScreen.main.bounds.width/2, height: 30)
collectionView.center(x: centerXAnchor, y: centerYAnchor)
collectionView.backgroundColor = .clear
collectionView.isUserInteractionEnabled = true
collectionView.clipsToBounds = true
collectionView.allowsSelection = true
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(HomeMenuCell.self, forCellWithReuseIdentifier: "cellID")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sections.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! HomeMenuCell
cell.title = sections[indexPath.row]
cell.isSelected = selectedIndex == indexPath.row
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let currentCell = collectionView.cellForItem(at: IndexPath(row: selectedIndex, section: 0)) as? HomeMenuCell else { return }
guard let newCell = collectionView.cellForItem(at: indexPath) as? HomeMenuCell else { return }
currentCell.isSelected = false
newCell.isSelected = true
selectedIndex = indexPath.row
delegate.didSelectSection(at: indexPath.row)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.width)/CGFloat(sections.count), height: collectionView.frame.height)
}
override func layoutSubviews() {
super.layoutSubviews()
collectionView.layer.cornerRadius = collectionView.bounds.height/2
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Your collection view cells cannot be selected because your collection view is outside the bounds of its super view.
Change the middle part of your create() func as follows (you didn't include your "constraint helpers" so I used standard constraint syntax):
addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: topAnchor, constant: 0.0),
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0),
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0.0),
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0),
collectionView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.5),
collectionView.heightAnchor.constraint(equalToConstant: 30.0),
])
//collectionView.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: UIScreen.main.bounds.width/2, height: 30)
//collectionView.center(x: centerXAnchor, y: centerYAnchor)
collectionView.backgroundColor = .clear
I want to implement UITableView's UISwipeActionsConfiguration for a UICollectionView. In order to do so, I am using SwipeCellKit - github
My UICollectionView adopts to the SwipeCollectionViewCellDelegate protocol. And the cell inherits from SwipeCollectionViewCell.
ViewController with UICollectionView
class SwipeViewController: UIViewController {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.register(SwipeableCollectionViewCell.self, forCellWithReuseIdentifier: SwipeableCollectionViewCell.identifier)
collectionView.showsVerticalScrollIndicator = false
collectionView.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 4, right: 0)
collectionView.backgroundColor = UIColor(white: 0.97, alpha: 1)
collectionView.dataSource = self
collectionView.delegate = self
return collectionView
}()
var items: [String] = {
var items = [String]()
for i in 1 ..< 20 {
items.append("Item \(i)")
}
return items
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.setConstraints(topAnchor: view.topAnchor,
leadingAnchor: view.leadingAnchor,
bottomAnchor: view.bottomAnchor,
trailingAnchor: view.trailingAnchor,
leadingConstant: 10,
trailingConstant: 10)
}
}
extension SwipeViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 80)
}
}
extension SwipeViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SwipeableCollectionViewCell.identifier, for: indexPath) as! SwipeableCollectionViewCell
cell.backgroundColor = BackgroundColor.colors[indexPath.row]
cell.delegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
extension SwipeViewController: SwipeCollectionViewCellDelegate {
func collectionView(_ collectionView: UICollectionView, editActionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
}
return [deleteAction]
}
}
SwipeCollectionViewCell
class SwipeableCollectionViewCell: SwipeCollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(nameLabel)
nameLabel.setConstraints(topAnchor: self.topAnchor,
leadingAnchor: self.leadingAnchor,
bottomAnchor: self.bottomAnchor,
trailingAnchor: self.trailingAnchor)
self.backgroundColor = .white
}
static let identifier = "TaskListTableViewCell"
private let nameLabel: UILabel = {
let label = UILabel()
label.text = "Simulator user has requested new graphics quality"
label.numberOfLines = 0
return label
}()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
After doing so, when I swipe the cell, the deleteAction overlaps with the content of the cell.
As you see in the screenshot, the cell's content overlaps with the deleteAction text.
Update
setConstraints sets views's translatesAutoresizingMaskIntoConstraints to false
You must add nameLabel to contentView.
Change
self.addSubview(nameLabel)
nameLabel.setConstraints(topAnchor: self.topAnchor,
leadingAnchor: self.leadingAnchor,
bottomAnchor: self.bottomAnchor,
trailingAnchor: self.trailingAnchor)
to
self.contentView.addSubview(nameLabel)
nameLabel.setConstraints(topAnchor: self.contentView.topAnchor,
leadingAnchor: self.contentView.leadingAnchor,
bottomAnchor: self.contentView.bottomAnchor,
trailingAnchor: self.contentView.trailingAnchor)
Result:
Basically, I have a UICollectionView within a UIView (that extends the collection view data and delegate protocols). Certain UICollectionView functions, such as numberofSections and numberOfItemsInSection are being called, while others such as cellForRowAt are not being called.
The collection view in question is rolesCollectionView, and its data is the roles array. I have added some print statements to see which functions are being called, and only the aforementioned ones are. I'm also certain that this isn't an issue with the RolesCollectionViewCell, whose code I can also provide if necessary.
class ProjectSearchViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var rolesCollectionView: UICollectionView!
var roles = ["Developer", "Designer", "Backend Developer", "Frontend Developer"]
override func viewDidLoad() {
super.viewDidLoad()
print("test")
// Do any additional setup after loading the view.
navigationItem.title = "Search Projects"
view.backgroundColor = .white
var rolesLayout = UICollectionViewFlowLayout()
rolesLayout.scrollDirection = .horizontal
rolesLayout.minimumLineSpacing = 8
rolesLayout.minimumInteritemSpacing = 8
rolesLayout.sectionInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
rolesLayout.estimatedItemSize = CGSize(width: 50, height: 25)
rolesCollectionView = UICollectionView(frame: .zero, collectionViewLayout: rolesLayout)
rolesCollectionView.backgroundColor = .white
rolesCollectionView.delegate = self
rolesCollectionView.dataSource = self
rolesCollectionView.register(RolesCollectionViewCell.self, forCellWithReuseIdentifier: "role")
rolesCollectionView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(rolesCollectionView)
rolesCollectionView.reloadData()
let newView = UIView()
newView.backgroundColor = .black
newView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(newView)
NSLayoutConstraint.activate([
newView.widthAnchor.constraint(equalToConstant: 50),
newView.heightAnchor.constraint(equalToConstant: 50),
newView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
newView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
setupConstraints()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// if collectionView == self.rolesCollectionView {
print("return")
print(roles.count)
return roles.count
// } else {
// return 0
// }
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
print("section")
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("cell for item at")
if collectionView == self.rolesCollectionView {
var cell = rolesCollectionView.dequeueReusableCell(withReuseIdentifier: "role", for: indexPath) as! RolesCollectionViewCell
cell.configure(roleName: roles[indexPath.row])
print(roles[indexPath.row])
return cell
} else {
return UICollectionViewCell()
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//if collectionView == self.rolesCollectionView {
print("size")
print("test: \(collectionView == rolesCollectionView)")
return CGSize(width: 15.0, height: 15.0)
// }
}
func setupConstraints() {
NSLayoutConstraint.activate([
rolesCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
rolesCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
rolesCollectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8)
//rolesCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bot)
])
}
}
I am trying to implement UICollectionView in custom keyboard extension but it is not working.
This is what I am doing inside viewDidLoad method.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
I have added UICollectionViewDelegateFlowLayout and UICollectionViewDataSource protocols to my class and then implemented dataSource methods as shown below
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.orangeColor()
return cell
}
The same code is working as expected in app bundle. I don't understand why it has a different behaviour in keyboard extension.
Is there anything that I am missing here, What do I need to do to get it working in keyboard extension?
The collection view will be visible when you provide constraints for the collectionView.
Code :
collectionView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(collectionView)
collectionView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor,constant: 0.0).active = true
collectionView.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: 0.0).active = true
collectionView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
collectionView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
Try this once bro:
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 10, height: 10)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
// Perform custom UI setup here
self.nextKeyboardButton = UIButton(type: .System)
self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), forState: .Normal)
self.nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
self.view.addSubview(self.nextKeyboardButton)
let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(textInput: UITextInput?) {
// The app is about to change the document's contents. Perform any preparation here.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.orangeColor()
return cell
}
override func textDidChange(textInput: UITextInput?) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
textColor = UIColor.whiteColor()
} else {
textColor = UIColor.blackColor()
}
self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)`}`