Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UiScrollview with nested image looks weird

I have a UIScrollView inside a UIViewController (subclassed by ImageViewController). The ViewController itself is part of a NavigationController's stack. Now, apart from having a navigation bar, I want the ScrollView to take all of the available room on the screen. The UIImageView inside the scrollview should then fill the available room of the scroll view. You can see the current state at the bottom of this posting.

class ImageViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    var imageView: UIImageView?
    var image: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.delegate = self

        if let image = image {
            imageView = UIImageView(image: image)
            if let imageView = imageView {
                imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image.size)
                scrollView.addSubview(imageView)
                scrollView.contentSize = image.size

                let scaleHeight = scrollView.frame.size.height / scrollView.contentSize.height
                let scaleWidth = scrollView.frame.size.width / scrollView.contentSize.width
                let minimumScale:CGFloat = min(scaleHeight, scaleWidth)
                let maximumScale:CGFloat = max(scaleHeight, scaleWidth)

                scrollView.minimumZoomScale = minimumScale
                scrollView.maximumZoomScale = maximumScale
                scrollView.zoomScale = maximumScale
            }
        }
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}

The code leaves me with unnecessary borders (left, right, top). How do I get rid of them?

enter image description here

EDIT: With @Bxtr's suggestion and another stackoverflow thread I was able to remove the borders left and right to the scroll view. After some more digging I found out that by deactivating Adjust Scroll View Insets, the image inside the scroll view can be correctly vertically positioned. Still, I do not get the reason for the vertical misplacement in the first place...

like image 811
Bastian Avatar asked Jul 02 '15 14:07

Bastian


1 Answers

Have you checked the margin/padding values, because it kinda looks so (same size on left and right border). If it is not the case, could you please also post your xml file of the activity so we can have every part of the puzzle to help you ?

like image 143
Bxtr Avatar answered Oct 12 '22 22:10

Bxtr