Swift ScrollView In UITableViewCell not scrolling - ios

I am new to swift and am trying to add a UIScrollview to a custom UITableViewCell. I can't seem to get the scrollview to scroll in the simulator though. I have tested both vertical and horizontal scrolling.
Here is the code for my custom UITableViewCell
import UIKit
class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var winLoseValueLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var newTotalLabel: UILabel!
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var playersLabel: UILabel!
#IBOutlet weak var playersScrollView: UIScrollView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.playersLabel.text = nil
//self.playersScrollView.contentSize.height = 1000
self.playersScrollView.contentSize.width = 1000
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func addLabelToScrollView(str : String) {
if self.playersLabel.text == nil{
self.playersLabel.text = str
}
else{
self.playersLabel?.text = self.playersLabel.text! + "\n\(str)"
}
}
}
My playersScrollView outlet has been properly connected to the scrollview in my storyboard. As far as I can see from all of the tutorials and sources online, I should just have to set the contentSize width or height (larger than the view area) to get it to scroll, which I have done in the awakeFromNib function.
Any ideas why this is not working in the simulator? Secondly, ideally I would like to have a vertical scrolling scrollview in the table cell, but I thought that the table view's vertical scrolling might have been interfering with the scrollview, so I changed it for horizontal scrolling for testing, which also didn't work.
Can you have a vertical scrollview in a vertically scrolling UITableView, or will the table view scrolling interfere too much with the scrollview?

Ok, it turns out that the constraints on my playersLabel, which is a subview of playersScrollView, was the culprit, although I am not sure why. I adjusted the constraints on the label and that allowed the horizontal scrolling to work.
The vertical scrolling still did not work, so I had to override the touchesBegan and touchesEnded methods in the scroll view so I could disable/re-enable the scrolling of the table view.

Related

InputAccessoryView with custom view created in IB

I've read a lot of material on this topic but most of them create custom view programatically.
Is it possible to use InputAccessoryView with a custom view created in IB? In storyboard I've added textInputView, inside which I've added text view and send button etc as seen in the screenshot below.
I've following code so which removes Table View for some reason so I can't get it working. I've added tableview in the storyboard.
I've shown here only InputAccessoryView related code.
class InputAccViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var textView: UITextView!
#IBOutlet weak var textInputView: UIView!
// other code
override var canBecomeFirstResponder: Bool { return true }
override var inputAccessoryView:UIView {
get{
return self.textInputView
}
}
override func viewDidLoad() {
super.viewDidLoad()
textInputView.removeFromSuperview()
// other code
}
// other code tableview delegates etc...
}
Left screenshot is with the accessory view code which doesn't show table view. If I comment out accessory view related code it does show table view as in the right screenshot.
Seems you might be constraining the UITableView's bottom to the textInputView's top. When you are setting the textInputView as the inputAccessoryView of the UIViewController this no longer works as expected. When setting the textInputView as the inputAccessoryView make sure you constraint the bottom of UITableView to the bottom of UIViewController's view.

autoResizingMask for inputAccessoryView not working

I'm trying to cope with iPhone X with inputAccessoryView. I've added a view to my ViewController like so:
CustomView
I've defined an outlet and attached to it. Returned the same view as inputAccessoryView like so:
class ViewController: UIViewController {
#IBOutlet weak var textFieldContainer: UIView!
override var inputAccessoryView: UIView? {
return textFieldContainer
}
override var canBecomeFirstResponder: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
textFieldContainer.autoresizesSubviews = true
}
}
I've made sure to add constraints relative to safe area. Here are my constraints:
Constraints
Adjusted autoResizingMask in SB like so:
AutoResizingMask
However, it's still not working. Here's the output:
Output
What did I miss?
Unlike for table view cells, there is no support for dynamic height calculation in input accessory views, as far as I know.
You could use a fixed height for the accessory view.
But I assume, that you want just to change either top, bottom, or height constraint in interface builder and the change is reflected after the next build.
What you could do is, to use a custom view class where you connect your top, bottom and height constraints.
Then override intrinsicContentSize and return the sum of the three constraint constants.
class TextFieldContainer: UIView {
#IBOutlet weak var topConstraint: NSLayoutConstraint!
#IBOutlet weak var bottomConstraint: NSLayoutConstraint!
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
override var intrinsicContentSize: CGSize {
let contentHeight =
self.topConstraint.constant
+ self.heightConstraint.constant
+ self.bottomConstraint.constant
return CGSize(width: UIScreen.main.bounds.width, height: contentHeight)
}
}
Your layout hierarchy could be simplified and look like this:
The autoresizing mask could look like this:
The final result would behave like the following then:
You're giving textfield height and also bottom constraint, try to remove bottom constraint for textfield

Swift: stop image inside scroll from moving up and down

I am working on a sample to display images inside scroll. The image is loading fine but then it allows me to drag the image up and down inside the scroll. I would still want to be able to move right and left (horizontally), but the image should be fixed from moving vertically.
The scroll view and the image constraints are set to the top, bottom, left and right as those of the main view.
What am I doing wrong?
This is my code and storyboard settings
import UIKit
class Image2ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.scrollView.contentSize = (imageView.image?.size)!
}
}
and these are my storyboard settings for ScrollView and ImageView. The top 2 are for the scroll and other 2 are for the image.
Adding this line in the viewDidload solved the issue:
self.automaticallyAdjustsScrollViewInsets = false

2 Round Corners of a UIView in UITableViewCell not working

I am trying to create round corners for a UIView with in a custom TableViewCell. The problem is when the table view loads, it only rounds the Top Left corner of the view and not the bottom one. Now when i scroll the table view little bit, it rounds the bottom left part of the view as well.
I have tried every possible method but i can't get my head around it. I am also attaching the screenshots as well as copying the code. Thanks
The code that i am using in the custom view cell class is below.
class FoodTVCell: UITableViewCell {
var food: Food!
#IBOutlet weak var foodPicture: UIImageView!
#IBOutlet weak var foodName: UILabel!
#IBOutlet weak var foodRating: UIImageView!
#IBOutlet weak var deliveryTime: UILabel!
#IBOutlet weak var minOrder: UILabel!
#IBOutlet weak var category: UILabel!
#IBOutlet weak var foodPriceLabelBG: UIView!
#IBOutlet weak var foodPrice: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
foodPicture.layer.cornerRadius = 75/2
foodPicture.clipsToBounds = true
}
override func layoutSubviews() {
super.layoutSubviews()
let maskLayer = CAShapeLayer()
let corners = UIRectCorner.TopLeft.union(UIRectCorner.BottomLeft)
maskLayer.path = UIBezierPath(roundedRect: foodPriceLabelBG.bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(20.0, 20.0)).CGPath
foodPriceLabelBG.layer.mask = maskLayer
foodPriceLabelBG.clipsToBounds = true
}
}
Your 'corner rounding' code is working very well, the problem is that your UIView's bottom edge is going outside of your Cell's bottom edge. Decrease your UIView's Y position and it will be covered by your Cell. I see your cell's separator line invisible too, that means you should give larger height to your Cell at heightForRowAtIndexPath:
You said it's working very well on scrolling, I suppose it's working when scrolling down and not working when scrolling to top. It means that your first cell was redrawn after second while scrolling to the bottom (where the second cell doesn't cover your first cell), and vice versa while scrolling to the top (where second cell covers your first cell)

Get number of column in a CollectionView IOS

I use an UICollectionView in my first app. My problem is I can't get the number of column (or line) calculated in the UICollectionView.
The UICollectionView is inside an UIScrollView, and I want to set the height of the UICollectionView because I don't want to scroll in the UICollectionView but I want to see all the items.
Is this possible?
Thanks,
F.G
After many researches I found the solution:
I don't need to know the number of column or line in the collection view if I want to fix the height.
The height of the collection view can be updated automatically:
Add a height constrain to the collection view
Add the CollectionView and the constrain as outlets in the ViewController:
#IBOutlet weak var heightConstrainCollectionView: NSLayoutConstraint!
#IBOutlet weak var myCollectionView: UICollectionView!
After the view Load, update the height of the CollectionView:
override func viewDidAppear(animated: Bool) {
super.viewDidLoad()
heightConstrainCollectionView.constant = myCollectionView.contentSize.height
}
If the user rotate the device, update the CollectionView's height:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
heightConstrainCollectionView.constant = myCollectionView.contentSize.height
// Add this if you want to see the height after rotation
println("New Width: \(newspaperCollectionView.contentSize.height)")
}
Now I can't scroll in my CollectionView and the CollectionView show all the items

Resources