ScrollView contentSize Larger than Bounds, Yet No Scrolling - ios

I have a scrollView containing a single image. The image's bounds are greater than the scrollView's frame. I have set the scrollView's contentSize to be equal to the image's bounds.
Yet no scrolling. I have no idea what could be causing this.
override func viewDidLoad() {
super.viewDidLoad()
var matches = SortThread.getSortThread().retrieveMatches()
scrollView.frame = CGRectMake(0, navigationController!.navigationBar.frame.height - 20, UIScreen.mainScreen().bounds.width, view.bounds.height - (navigationController!.navigationBar.frame.height - 20))
var imageName = String()
imageName = matches[0].pictures![0]
var parsedImageName = imageName.componentsSeparatedByString(".") as [String]
var newImageName: String = parsedImageName[0] as String
let path = NSBundle.mainBundle().pathForResource("MVLMP Images (Resized)/" + newImageName, ofType: "jpg")
var image = UIImage()
image = UIImage(contentsOfFile: path!)!
var imageView = UIImageView(image: image)
var ratio: CGFloat = image.size.width/image.size.height
var screenHeight: CGFloat = UIScreen.mainScreen().bounds.height - navigationController!.navigationBar.frame.height - 20
if(ratio > 1){
imageView.frame = CGRectMake(0, 0, ((scrollView.bounds.width)/7) * 6, 0)
imageView.frame = CGRectMake(0, 0, imageView.frame.width, imageView.frame.width * ratio)
} else {
imageView.frame = CGRectMake(0, 0, 0, (screenHeight/9)*8)
imageView.frame = CGRectMake(0, 0, imageView.frame.height * ratio, imageView.frame.height)
}
scrollView.addSubview(imageView)
scrollView.contentSize.width = imageView.frame.width
scrollView.contentSize.height = imageView.frame.height
}
}
I've printed out the scrollView.contentSize.width and the scrollView.bounds.width and the content width is in fact greater than the bounds width by about 40.

As Muc Dong commented, I was adding the width of the images to the scrollView's contentSize.width. This was not necessarily the correct thing to do as in my case there is some margin between the images that I was not accounting for. This margin should of been included in the additions to to scrollView.contentSize.width.

Related

Swift :- How to add offset to UIImageview inside UIScrollview in Swift?

In my iOS app, I have a scroll view which has the image view to scroll and zoom images.
What I am unable to do is launch the image view with pre-defined offset and zoom value.
Right now, on launch, the original image is shown which I can zoom-in/out and scroll, but I wanted to zoom-in/out with an offset with default values.
On setting the scrollview.zoomScale, I am not able to scroll the image completely.
Code below:-
let scroller = UIScrollView()
scroller.translatesAutoresizingMaskIntoConstraints = false
scroller.minimumZoomScale = 1
scroller.maximumZoomScale = 5
return scroller
}()
let imageView: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFit
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()
The first step:
scrollView = UIScrollView(frame: view.bounds)
view.addSubview(scrollView)
imageView.center = view.center
scrollView.addSubview(imageView)
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 2.0
scrollView.delegate = self
Step 2: implement the UIScrollViewDelegate method
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
Finally:
private func setImagePosition(_ image: UIImage) {
imageView.image = image
let size = imageSize(WithScreen: image)
imageView.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
scrollView?.contentSize = size
if let scrollView = scrollView, size.height < scrollView.bounds.size.height {
let offsetY = (scrollView.bounds.size.height - size.height) * 0.5
scrollView.contentInset = UIEdgeInsets(top: offsetY, left: 0, bottom: offsetY, right: 0)
}
}
private func imageSize(WithScreen image: UIImage) -> CGSize {
var size = UIScreen.main.bounds.size
size.height = image.size.height * size.width / image.size.width
return size
}

Set frame CGReact x position in middle like centerXAnchor NSLayoutConstraint programmatically

I am creating a simple app which set image as circle by calculate with frame of image.
Here is I declare image variable:
lazy var imageUserDetailProfileView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.layer.borderWidth = 1
imageView.image = UIImage(named: "profile-icon")
imageView.layer.borderColor = UIColor(red:0.00, green:0.50, blue:0.00, alpha:1.0).cgColor
imageView.clipsToBounds = true
return imageView
}()
Here is i made that frame width and height of image became circle
imageUserDetailProfileView.frame = CGRect(x: view.frame.midX, y: 20,width: 100, height: 100)
imageUserDetailProfileView.layer.cornerRadius = imageUserDetailProfileView.frame.height/2
Now i want image should stay in middle of screen like using NSLayoutConstraint which has method centerXAnchor.
How to solve this problem?
let midX = view.frame.size.width / 2
let midY = view.frame.size.height / 2
Try This Code.
Custom Method For Image Set In Center Of Any View. Just Call Method And Pass Image & View. Customize according to Use. Swift 4
func setImageInCenterOfView(image:UIImage,view:UIView) {
let imageWidth:CGFloat = 100
let myImageView = UIImageView.init(frame: CGRect.init(x: (view.frame.size.width/2)-imageWidth/2, y: (view.frame.size.height/2)-imageWidth/2, width: imageWidth, height: imageWidth))
myImageView.clipsToBounds = true
myImageView.layer.cornerRadius = myImageView.frame.size.height/2
myImageView.image = image
view.addSubview(myImageView)
view.layoutIfNeeded()
}

how to allow infinite horizontal scrolling in swift 3 scrollView

Let say I have 3 images in the scroll view. Now I am able to scroll it horizontally in either way. However, when reached the image #3, how to allow it to continuous scroll to image #1.
override func viewDidAppear(_ animated: Bool) {
//allow scrolling even outside the scroll view
view.addGestureRecognizer(scrollView.panGestureRecognizer)
var contentWidth : CGFloat = 0.0
let scrollWidth = scrollView.frame.size.width
// append 3 images
for x in 0...2 {
let image = UIImage(named: "icon\(x).png")
let ImageView = (UIImageView(image:image))
images.append(ImageView)
var newX : CGFloat = 0.0
newX = scrollWidth/2 + scrollWidth * CGFloat(x)
scrollView.addSubview(ImageView)
//set the position of the image to center of the screen
ImageView.frame = CGRect(x: newX - 75, y: (scrollView.frame.size.height/2)-75, width: 150, height: 150)
contentWidth += newX
}
scrollView.clipsToBounds = false
scrollView.contentSize = CGSize(width: contentWidth, height: view.frame.size.height)
}

scrollView images are not in the center using midX?

I used two statements in my code when initializing newX variable which I believe they should give the same result but one didn't, which are:
( scrollView.frame.size.width / 2 ) - 75
And
scrollView.frame.midX - 75
here is my code, look for the comment at the middle:
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
var images = [UIImageView]()
var contentWidth: CGFloat = 0.0
override func viewDidAppear(_ animated: Bool) {
for i in 0...2 {
let image = UIImage(named: "icon\(i).png")
let imageView = UIImageView(image: image)
// Here is the problem
let newX = (scrollView.frame.size.width / 2 + scrollView.frame.size.width * CGFloat(i)) - 75
let newY = scrollView.frame.midY - 75
imageView.frame = CGRect(x: newX, y: newY, width: 150, height: 150)
scrollView.addSubview(imageView)
contentWidth += scrollView.frame.size.width
}
scrollView.contentSize = CGSize(width: contentWidth, height: view.frame.size.height)
scrollView.clipsToBounds = false
scrollView.backgroundColor = UIColor.purple
}}
The first one perfectly centred the images, but when I replaced it with the second on it didn't center them as shown in the picture here
Code Representation . why?
The difference is that midX is considering also the position of the scrollview's origin, so midX would be half the width of your scrollView + the X origin of your scrollView. (Similar to how minX is not always 0)
Width, on the other hand, is, well, the width of the scrollView, which is absolute.

My scrollView cut my last picture Swift 3

I have an horizontal scrollView, where I add my array of image, but it alway cut the picture, my content mode is scaleToFill. I printed the size of my imageView and my scrollView and gave me the same size, so why can't I see my picture filling all my scrollView
My first Image and
My last image of my array
Here is my code
func getFavoriteSalon()
{
arrayOfDataScrollView = [#imageLiteral(resourceName: "large"), #imageLiteral(resourceName: "Kristoff-image-kristoff-36081750-500-266"), #imageLiteral(resourceName: "image-battle-for-dream-island-39780938-500-266")]
mainScrollView.contentSize.width = (mainScrollView.frame.width) * CGFloat(arrayOfDataScrollView.count)
for i in 0..<arrayOfDataScrollView.count
{
let imageView = UIImageView()
let xPosition = self.mainScrollView.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width , height: self.mainScrollView.frame.height)
imageView.image = arrayOfDataScrollView[i]
imageView.contentMode = .scaleToFill
print(self.mainScrollView.frame.width) =>Give me 375
print(self.mainScrollView.frame.height) =>Give me 200
print("Width => \(imageView.frame.width)") =>Give me 375
print("height => \(imageView.frame.height)") =>Give me 200
mainScrollView.addSubview(imageView)
}
}
I found a solution, for me it's a bug xcode, i work with an emulateur of Iphone7+, but I got the value of an Iphone 7 when i print my variables, Be sure to have the preview, and the emulateur looking the same device
Use this and set content size according image
var xPosition:CGFloat = 0
for i in 0..<arrayOfDataScrollView.count{
var frame: CGRect = CGRect(x: 0, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
frame.origin.x = CGFloat(xPosition)
frame.size = mainScrollView.frame.size
let imageView = UIImageView(frame: frame)
imageView.contentMode = .scaleToFill
imageView.frame.size.width = imageWidth
imageView.frame.size.height = imageHeight
imageView.frame.origin.x = xPosition
imageView.frame.origin.y = 0
mainScrollView.addSubview(imageView)
xPosition += imageView.frame.size.width + 5; (5 you can change this value)
mainScrollView.contentSize = CGSize(width: CGFloat(i) * imageView.frame.size.width, height:
mainScrollView.frame.size.height)
}

Resources