Did select on table view cell does not response - ios

The problem is that I have a tableview with webview
Inside each cell and the content of webview can scroll
Horizontally, but not vertically. It’s height is the same as
Cells height. So the problem is when I am trying to detect
DidSelect of tableview cell it doesn’t response. But when I tried to put userinteraction of webview to false it works but webview can not scroll anymore. Is there any alternative ways of doing this. Btw webview cells must contain webview no other ways.
Here is the screenshot of why I need webview inside cell.
Each cell contains webview content

You can add UITapGestureRecognizer to the WebView and do the action inside the UITapGestureRecognizer. (Here I'm adding how to add a gesture recognizer, you have to adapt it to your own case)
let tapRecognizer = UITapGestureRecognizer(target: webView, action: #selector(self.handleSingleTap(_:)))
tapRecognizer.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapRecognizer)
func handleSingleTap(recognizer: UITapGestureRecognizer) {
//Do something here with the gesture
}

you can set delay touch false in order to have select event for cell.
tableview.delaysContentTouches = false
use this link for detail info - https://developer.apple.com/documentation/uikit/uiscrollview/1619398-delayscontenttouches

Related

Gesture recogniser is impacting other functionality

I have a gesture recogniser that is stopping the collectionView function from working properly. Example code is added below.
The view controller has a collection view, where cells can be deleted in a similar way to how apps are deleted on an iPhone home screen. The user long presses the screen for the collection cells to start shaking and stops shaking once the screen is tapped (not on the delete button for the cells).
Now that this delete functionality is working, I can no longer click on the cells to open a new view controller with the associated information. Is there a way to add a conditional so that the tapRecognizer only happens while the cells are shaking, so that the collectionView function works when a cell in the collection view is tapped?
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapPressed))
self.collectionView.addGestureRecognizer(longPressRecognizer)
self.collectionView.addGestureRecognizer(tapRecognizer)
}
//Tap screen
#objc func tapPressed(sender: UITapGestureRecognizer) {
stopCellShaking()
}
//Open the information for the selected cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//Function to open new screen
showInformation(chosenCell: exampleList[indexPath.row])
}
Is there a way to add a conditional so that the tapRecognizer only happens while the cells are shaking
Absolutely. In general, just turn off the tap recognizer by setting its isEnabled to false. When the shaking starts (long press), set the tap gesture recognizer's isEnabled to true.

iOS with UICollectionView how to achieve this type of layout

Requirement:
I should be able to scroll entire view if left view is visible or not (all components should scroll at a time if i scroll anywhere with in the view).
By clicking on show/hide left view button button it should be able to hide or show left view.
In cell (only cell not left view) there is a expand/collapse functionality (i can increase/decrease cell height)
And If i change font size in device settings app, it should be effect here also(So supporting dynamic font size)
What i have tried:
I have tried with tableview, scroll view but no luck. Finally want to try with collection view, can any one please help me out how to proceed with collection view. And in future do we get any complications if we use collection view.
Best Approach.
Your UI hierarchy should be like this.
StackView
CollectionView
TableView
Note: You can take both CollectionView or TableView but I prefer both different so that I don't need to put condition in delegates & datasource and I can manage easily. Choice is your's what you like to prefer.
Now your UI design looks like this
Green color button is used to show hide your left collectionview (you mentioned in your post).
Set your datas in CollectionView and TableView as per your requirement.
To toggle left menu, just use below one line code on greenButton action.
#IBAction func btnToggle(_ sender: Any) {
colView.isHidden = !colView.isHidden
}
For simple animation
#IBAction func btnToggle(_ sender: Any) {
UIView.animate(withDuration: 0.3) {
self.colView.isHidden = !self.colView.isHidden
}
}
Output:
Edit
You can take stackView in scrollview and turn off colview, tblView scrolling. Check below :
UI heirarchy
Additional code work
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
colView.isScrollEnabled = false
tblView.isScrollEnabled = false
colView.reloadData()
tblView.reloadData()
scrollView.contentSize = CGSize(width: self.view.frame.width,
height: max(colView.contentSize.height, tblView.contentSize.height))
stackHeight.constant = scrollView.contentSize.height
}
Note : It may cause some unexpected output (can be / can not be), so you need to take care of it.
Output:
What you need is UICollectionView with custom layout. You can achieve the effect you need using this approach.
There is tone of tutorials how to implement own custom layout.
Here is one of them

TableCell pangesture got ignore when inside a collectionview

Question and situation explaination
Hi I have a custom collection view that I make each collectionview cell the size of the screen and make it scroll horizontally so I get like a paging effect.
In each collectionview cell I have a tableview which also take up the whole size of that collectionview cell. I'm trying to implement trailing and leading swipe action of my tableview cell but it is being ignore because of the swipe gesture to scroll the collectionview cells.
Is there a way to maybe like set the collectionview cell to only watch for pangestures that are near the edge of the screen then scroll the collectionview, other wise the pan gesture will just be send to the tableview instead?
Alternative solution (temporary work around)
Though I have found a work around but I still very much interested in finding out if what I'm trying to do above is possible so if you know please leave your answer
So after searching and searching stackoverflow there seem to be other people with the same problem as mine but doesnt seem to be able to found an answer. Thus I have come up with another idea and I like to share so if anyone come across this problem they can have options to choose from.
My idea is adding a long press gesture regconizer for the tableview you can achieve that as follow in swift 4
func setupLongPressGesture() {
let longPressGesture: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
dataDisplayTable.addGestureRecognizer(longPressGesture)
}
#objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
if gestureRecognizer.state == .began {
let touchPoint = gestureRecognizer.location(in: dataDisplayTable)
if let indexPath = dataDisplayTable.indexPathForRow(at: touchPoint) {
// let cell = dataDisplayTable.cellForRow(at: indexPath) - use this to retreive the cell at the above indexPath to do something with it
// print(cell?.reuseIdentifier) - use this to retreive the cell identifier and do something with it
}
}
}
Then add this to your viewDidLoad()
setupLongPressGesture()

how can I expand the size of UITextView when user starts typing in it?

In my storyboard I have a UIViewController that contains an image view and a text view:
In my class I already handle a tap gesture - when user taps anywhere outside of the text view - the keyboard disappears:
override func viewDidLoad(){
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
Now, as you can see on my screenshot, the textview is pretty low, so when user taps it and the keyboard appears - it covers part of the textview itself. I would like to either move it up so it's more visible above the keyboard or - preferably, if that's even possible - for the time of editing - expand the size of textview so it covers the rest of the screen (and comes back to its normal size after user is done with editing). Can you help me with that?
I think the better approach is to slide your View above keyboard. Here's an answer how to do so.
Use IQKeyboardManager
It will handle everything regarding text field.
https://github.com/hackiftekhar/IQKeyboardManager

Switching to UIPanGestureRecognizer when TableView reaches it top

I have a UITableView added to UIViewController. Behind that TableView there is UIImageView like this:
The default behavior of a UITableView is to show whitespace when scrolling up and there is no more content to show. I would like to override this and switch to custom UIPanGestureRecognizer that will move the whole tableview down.
To clarify: When the tableview reaches it top I would like to move the entire tableview instead.
I have set up my PanGestureRecognizer and added it to the tableview and tried this:
func scrollViewDidScroll(scrollView: UIScrollView) {
if self.tableView.contentOffset.y == 0{ //tableview reaches its top
panGestureRecognizer.enabled = true
}else{
panGestureRecognizer.enabled = false
}
}
This does not work at all since the the scrollViewDidScroll triggers at the end of the gesture.
Any other ideas on how I can implement this?

Resources