I embedded a UICollectionView in a UIViewController and tried to add a UIRefreshControl into the UICollectionView with these code:
override func viewDidLoad() {
......
// Create the UIRefreshControl.
let pullDownLogo = UIRefreshControl()
pullDownLogo.clipsToBounds = true
// Create a UIImageView.
let pullDownImgView = UIImageView(frame: pullDownLogo.bounds)
pullDownImgView.image = UIImage(named: "fa-heart")
pullDownImgView.contentMode = UIViewContentMode.ScaleAspectFit
pullDownImgView.clipsToBounds = true
// Add the UIImageView into UIRefreshControl.
pullDownLogo.addSubview(pullDownImgView)
WatchListCollectionView.addSubview(pullDownLogo)
......
}
When I pulled down the collection view and not reached the bottom, the image did show.
The problem is: the image overlapped and blocked the collectionView cell.
What I want is the image in the refresh controller be blocked by the cell content.
The problem is shown in this snapshot:
Related
I am trying to figure out how to add pinch/zoom capabilities to an imageView,
but specifically to one that I added to the scrollView programmatically. I was able to find some great examples on SO to pinch and zoom an image using a scrollview with viewForZooming, but in that instance I had to return the image view being pinched and zoomed, which doesn't work if I am returning it programatically.
My ultimate goal is to have an array of images where the user can scroll left and right to see all the images AND be able to zoom in on them, basically just as if they were flipping through their photoStream. I found an ok tutorial for adding the images dynamically for scrolling here https://www.codementor.io/taiwoadedotun/ios-swift-implementing-photos-app-image-scrolling-with-scroll-views-bkbcmrgz5#comments-bkbcmrgz5 but I am not clear how to add the viewForZooming since the image views are being .addSubview dynamically in a loop.
I created a little example with a collection view of 0-n images associated to a post. Once the collectionViewCell with the image is tapped a hidden scrollView appears with a new dynamic UIImageView added as a subView. All works great but I don't know how to add the pinch/zoom now.
#objc func imageTapped(_ sender: UITapGestureRecognizer) {
print("BlipeFile Image Tapped")
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
//newImageView.frame = UIScreen.main.bounds
newImageView.contentMode = .scaleAspectFit
newImageView.clipsToBounds = true
newImageView.layer.borderColor = UIColor.gray.cgColor
newImageView.layer.borderWidth = 3.0
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.isUserInteractionEnabled = true
newImageView.image = imageView.image
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
scroller.isHidden = false
scroller.addSubview(newImageView)
}
#objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
scroller.isHidden = true
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
sender.view?.removeFromSuperview()
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return image //Can't return dynamically created newImageView?
}
My ultimate goal is to have an array of images where the user can scroll left and right to see all the images AND be able to zoom in on them
Apple has explained many times, in WWDC videos and in sample code that you can download, how this is done. Basically it’s just a scroll view in a scroll view. The outer scroll view permits scrolling horizontally. The inner scroll view contains an image view and permits zooming.
So instead of adding an image view, add a scroll view containing an image view.
Edit1: I am not using a storyboard everything has been added programmatically
Edit2: Updated code snippet
Hello, i've got the following issues with UICollectionView
The CollectionView won't allow you to scroll no matter what you do.
Sometimes the cells even disappear complete leaving you with a white background CollectionView.
The problem occurs when you try to scroll up to view the cells that are not visible.
I've create a CollectionView which has different type of cells.
The CollectionView is nested inside a ViewController below an ImageView.
Constraints are added and they work just fine.
How do i make it scrollable?
GIF representing the problem
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationItem.title = "Featured"
self.view.backgroundColor = UIColor.white
// Add UImage carousel
let carousel: UIImageView = {
let imageView = UIImageView()
let image = UIImage()
imageView.backgroundColor = UIColor.purple
return imageView
}()
self.view.addSubview(carousel)
carousel.translatesAutoresizingMaskIntoConstraints = false
// Add CollectionView
let featuredControllerLayout = UICollectionViewFlowLayout()
// Add CollectionViewController
featuredControllerLayout.scrollDirection = .vertical
let featuredController = FeaturedCollectionViewController(collectionViewLayout: featuredControllerLayout)
guard let featuredView = featuredController.collectionView else { return }
self.view.addSubview(featuredView)
featuredView.translatesAutoresizingMaskIntoConstraints = false
// Setup Constraints
if #available(iOS 11.0, *) {
let guide = self.view.safeAreaLayoutGuide
let guideSize = guide.layoutFrame.size
carousel.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
carousel.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
carousel.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
carousel.frame.size.height = guideSize.width/2
carousel.heightAnchor.constraint(equalToConstant: carousel.frame.size.height).isActive = true
featuredView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
featuredView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
featuredView.topAnchor.constraint(equalTo: carousel.bottomAnchor).isActive = true
featuredView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
}
}
Heyy what you are telling is you are not able to scroll up and see the entire collection view items ?
if that is the case then you have not accounted the height of tabbar controller so you can enable translucent for example I have a git repo I made you can check and let me know if you are stuck anywhere
https://github.com/dwivediashish00/socialApp
I am trying to add two custom interfaces that are UIViews to a UIScrollView in my mainStoryBoard.
both UIViews have an image inside them, the image's dimensions are equal to the view's dimensions.
This is the code I am using to add these views to my scrollView
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.myScrollView.frame = view.frame
for i in 0..<2{
let uiImage = UIImage(named: "\(i)")!
let view = Bundle.main.loadNibNamed("TestView", owner: self, options: nil)?.first as! UIView
view.setImage(uiImage)
let xpos = self.myScrollView.frame.width * CGFloat(i)
view.frame = CGRect(x: xpos, y: 0, width: self.myScrollView.frame.width, height: self.myScrollView.frame.height)
myScrollView.contentSize.width = self.myScrollView.frame.width * CGFloat((i + 1))
myScrollView.layer.cornerRadius = 20
myScrollView.layer.masksToBounds = true
myScrollView.addSubview(view)
}
}
}
In the TestView.swift, I have
func setImage(_ name:UIImage) -> Void{
testUIImage.image = name
testUIImage.contentMode = .scaleAspectFit
testUIImage.clipsToBounds = true
}
But when I run the app, the first View gets added fine, but when i scroll to the right, I can see that the second View is not in the Scrollview properly and there is a white space between the first and second View, almost like the space is an added view, that is pushing the second View to the right. I have paging enabled in the scrollViewer.
First Image In ScrollView
Second Image In ScrollView
As you can see, the second image is cut off on the leading edge.
This is the blank space between them:
enter image description here
I checked the width of both UIView frames, they are equal. I also checked the content size after the second iteration, it is equal to the size of both frames*2 , as it should be. But for some reason, a white black space is being added. And it only shows up after adding 2 or more subviews.
I have attached headerView to UITableView. And I also want to use pull to refresh with tableview.
I have attached headerview to tableview with this code:
tblView.tableHeaderView = headerView
and used redresh controll as:
var refreshControl: UIRefreshControl!
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(self.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
let tableController = UITableViewController()
self.addChildViewController(tableController)
tableController.tableView = self.tblView
tableController.refreshControl = self.refreshControl
Now issue is when I pull down tableview refreshcontrol showing but my header view position also changing with bounce effect. I don't want to change header position when I pulled down I want to display refreshcontrol under header view.
my header view and refreshcontrol is shown in screenshots. I want header view's position static joined with navigation bar but when user scroll up headerview will also have to scroll.
1)adding header view inside uiviewcontroller not to headerview for tableview,
2)you can change the headerframe accord scrollView of the tableView contentoffset,It is a little complex
3) github.com/CoderJackyHuang/StickyUpDownDemo this demo you can learn to solve your problem
Im adding a MKMapView to my tableview background in ViewdidLoad simply like this and its working.
override func viewDidLoad() {
super.viewDidLoad()
var myMapView = MKMapView(frame: CGRectMake(0, 0, 100, 500))
myMapView.mapType = MKMapType.Standard
self.tableView.backgroundView? = myMapView
}
This displays the map view in the background of the tableview but I wish for the MapView to only be displayed on the bottom of the tableview background and with the height of 250
Im guessing you have to add it as a subview and add constraints, could somebody show me some code as to how they would go about this?
Thanks!