I am trying to create a Collection View, which is somewhat like this...collection view -> 2 cells(vertically placed on screen) -> each cell contains some textFields -> click on the textField and enter your inputs.
I have added the textfields in the collection view cell file, and the cell is being called in a CollectionViewController. The problem is, the textfield are coming up fine on the UI part, but are not responding to touch. How can I solve this?
this is in a CollectionViewCell File (class AddressCollectionViewCell: UICollectionViewCell)
setTextFieldDelegates()
backgroundColor = .clear
contentView.addSubview(containerView)
containerView.backgroundColor = UIColor.black
containerView.fillSuperview()
//containerView.heightAnchor.constraint(equalToConstant: 600)
containerView.addSubview(addressStackView)
addressStackView.anchor(containerView.topAnchor, left: containerView.leftAnchor, right: containerView.rightAnchor, topConstant: 12, leftConstant: 12)
addTitleLabel()`
addTextFieldsToStackView()
func addTextFieldsToStackView() {
addressStackView.addArrangedSubview(houseTextField )
addressStackView.addArrangedSubview(pincodeTextField)
addressStackView.addArrangedSubview(localityTextField )
addressStackView.addArrangedSubview(areaTextField)
addressStackView.addArrangedSubview(addressLineOneTextField))
addressStackView.addArrangedSubview(cityTextField)
addressStackView.addArrangedSubview(stateTextField)
}
Related
I'm trying to add a custom UIView after adding a UICollectionView because I wanna show the custom bar view above the collection view. This is my code:
func loadFilters() {
let categoriesFlowLayout = UICollectionViewFlowLayout()
categoriesFlowLayout.scrollDirection = .vertical
categoriesCollection = UICollectionView(frame: CGRect(x: 10, y: getY(yAxis: searchField) + 10, width: view.frame.size.width - 20, height: (view.frame.size.height * 9 / 10) - getY(yAxis: searchField) - 10), collectionViewLayout: categoriesFlowLayout)
categoriesCollection.register(categoriesCell.self, forCellWithReuseIdentifier: "categoriesCell")
categoriesCollection.delegate = self
categoriesCollection.dataSource = self
categoriesCollection.backgroundColor = UIColor.clear
categoriesCollection.showsVerticalScrollIndicator = false
categoriesCollection.showsHorizontalScrollIndicator = false
view.addSubview(categoriesCollection)
addBar()
categoriesCollection.isUserInteractionEnabled = true
}
The addBar() function is declared in the custom superclass ViewController
if addBar() is called before view.addSubview(categoriesCollection) it looks like the image below but if it is called after then my collection view does not scroll or recognize touches. Is there anyway that will make the collection view scroll and bring the custom bar to front?
I've used sendSubviewToBack() and bringSubviewToFront() functions as well but the result is the same
When you insert a view above another view, the top view gets all touch events. So the UICollectionView does not receive any touch events anymore since another view is above it.
As I see from your post, you just want the bar at the bottom of the screen. So check the size of your custom UIView. It probably fills the entire screen and is completely above the UICollectionView. Just give the UIView some sort of background color and see, how much space it fills.
If this doesn't work, you can use a UIGestureRecognizer on the custom UIView and forward the touch events to the UICollectionView.
I added a view to tableView by using addSubview
let view = UIView(frame: CGRect(x: 52, y: ypos + 25, width: self.view.frame.width - 52, height: 1.5))
timeIndicatorView = view
self.eventCatalogTableView.addSubview(timeIndicatorView)
and then I use scrollToRow function on the tableView to scroll to the current Time.
func rollToCurrentTime() {
eventCatalogTableView.scrollToRow(at: IndexPath(row: Calendar.current.component(.hour, from: Date()), section: 0), at: UITableViewScrollPosition.middle, animated: false)
}
Everything works normally when I scroll to the row where the added view cannot be seen. Like this
However, when I scroll to the row where the added view is showed, the position of the view changes to a weird random position like this
In this case, when I scroll all the way up to the top and then scroll down, the view gets positioned in the right spot.
Is this an internal bug or is there any way to fix this issue?
the following code, click event works.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "SwiftCell")
let button = UIButton()
button.setTitle(hiddenGear ? "+" : "-", for: .normal)
button.setTitleColor(UIColor.lightGray, for: .normal)
button.addTarget(self, action:#selector(self.toggleGear), for: .touchUpInside)
button.frame = CGRect(x: self.view.frame.width - 36, y: 0, width: 30, height: 30)
button.backgroundColor = UIColor.red
cell.addSubview(button)
but what i want just like below:
move UIButton out of the uitableview cell, and the click event does not work.
button.frame = CGRect(x: self.view.frame.width - 36, y: -30, width: 30, height: 30)
try to use cell.bringSubview(toFront: button) , but it does not work too. have any idea?
This is illegal:
cell.addSubview(button)
You may not add a view to the cell. You must add the view only to the cell's contentView.
But the content view does not extend all the way to the right. If you want the view all the way over at the right, you must provide it as an accessory view.
Finally, you cannot easily add a working button outside the cell bounds, because a subview outside its superview's bounds is not touchable. You can make it touchable, however, by munging the hit-testing for the superview. You would need to override this method:
https://developer.apple.com/documentation/uikit/uiview/1622469-hittest
You need to use constraints, relative to the top of that UITableViewCell. Also, I can see you are trying to create a '+' (plus) icon in the corner. Consider using a Navigation Item found in the Object Library.
First it's not preferred to add UI elements inside cellForRow because of reusing , second you can encapsulate the button inside the cell and play with the background color to fake that it's out of cell if the button is supposed to be in every cell , otherwise you have to add it relative to the mainView (self.view)
I want to add multiple NSTextFields to NSScrollView dynamically.
Code:
func addLabel(data: String)
{
let offset = labelCount*20
let label = NSTextField(frame: NSMakeRect(0, CGFloat(offset), 200, 20))
label.stringValue = data
scrollView.documentView?.addSubview(label)
labelCount += 1
// scrollView is IBOutlet
// labelCount is var to keep record of number of labels
}
Problem:
It adds the NSTextFields correctly to the NSScrollView but they are only added till the visible part is available. After the visible part of NSScrollView is full, no more NSTextFields are added to it.
I need to add NSTextFields even though the visible part is full and scroll to find others.
How can I do it?
It's something like the following.
scrollView.documentView?.setFrameSize(CGSize(width: CGFloat(labelCount) * 200, height: 20))
I am trying to display a card but when the detailTextLabel gets too long, it causes the card to extend beyond the view. I'm not sure if this is a bug or not. I have adjusted the .numberOfLines to various numbers but that seems to have no effect.
// Detail label.
let detailLabel: UILabel = UILabel()
detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
detailLabel.numberOfLines = 0
cardView.detailLabel = detailLabel
EDIT
Here's the full code for the cardView.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cardView: CardView = CardView()
// Title label.
let titleLabel: UILabel = UILabel()
titleLabel.text = self.type[indexPath.row].capitalizedString
titleLabel.textColor = MaterialColor.blue.darken1
titleLabel.font = UIFont(name: "Roboto-Medium", size: 23)
cardView.titleLabel = titleLabel
// Detail label.
let detailLabel: UILabel = UILabel()
detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
detailLabel.numberOfLines = 100
cardView.detailLabel = detailLabel
// Yes button.
let btn1: FlatButton = FlatButton()
btn1.pulseColor = MaterialColor.blue.lighten1
btn1.pulseScale = false
btn1.setTitle("Ok", forState: .Normal)
btn1.setTitleColor(MaterialColor.blue.darken1, forState: .Normal)
// Add buttons to left side.
cardView.leftButtons = [btn1]
// To support orientation changes, use MaterialLayout.
view.addSubview(cardView)
cardView.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignFromTop(view, child: cardView, top: self.view.frame.height / 4)
MaterialLayout.alignToParentHorizontally(view, child: cardView, left: 10, right: 10)
}
}
So the issue is that you are using a UITableViewController. When you add the CardView to your view property, it is actually adding it to the UITableView, which will cause it to scroll when you scroll the tableView. The other issue of it not expanding correctly is due to the "|" value in AutoLayout, that seems to be breaking when placed in a TableView. This is probably a result of how the TableView is setup mathematically.
Either way, to avoid the scrolling issue, you will need to use a UIViewController and add a UITableView as a child view to it, or if you are keen on using the UITableViewController, it should be added as a child ViewController to a parent ViewController that you would add your CardView too. This will ultimately solve your bounds issue as well.
In the Material repo -> Examples/Programmatic/SideViewController example project, you can find a tableView added as a child view, and there, you would be able to place your CardView code as you have already done so in your current project.
I tried your code snippet in an example project and all seems to work. I used this code
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cardView: CardView = CardView()
// Title label.
let titleLabel: UILabel = UILabel()
titleLabel.text = "Activities dhajshd ajshd ashd kjashd jkasd jk asjkd asjk d"
titleLabel.textColor = MaterialColor.blue.darken1
titleLabel.font = UIFont(name: "Roboto-Medium", size: 23)
cardView.titleLabel = titleLabel
// Detail label.
let detailLabel: UILabel = UILabel()
detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
detailLabel.numberOfLines = 100
cardView.detailLabel = detailLabel
// Yes button.
let btn1: FlatButton = FlatButton()
btn1.pulseColor = MaterialColor.blue.lighten1
btn1.pulseScale = false
btn1.setTitle("Ok", forState: .Normal)
btn1.setTitleColor(MaterialColor.blue.darken1, forState: .Normal)
// Add buttons to left side.
cardView.leftButtons = [btn1]
// To support orientation changes, use MaterialLayout.
view.addSubview(cardView)
cardView.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignFromTop(view, child: cardView, top: self.view.frame.height / 4)
MaterialLayout.alignToParentHorizontally(view, child: cardView, left: 10, right: 10)
}
Both the title and detail labels are too long for a single line and the CardView responds appropriately.
So I have a suspicion that your "view" may be to wide. Can you confirm your "width" of the ViewController is at most the width of the device?