Reposition view which is going out of screen frame - ios

I have a imageView inside a UIView which displays on top of button when that button is tapped. The position of the button is dynamic.
If the button is on the extreme right of the screen, on tap of that button, half of the view which displays is going out of screen (i.e., only half is displayed)
In the code below, I'm setting my view's frame equal to image width and its height, imageView's position is (center of image is on top center of button )
Pls advice how i can reposition the such that the view will always inside the bounds. Below is the code Im using to display my custom view on top of button
//calculate frame for view
let imageHeight = myImage.size.height
let imageWidth = myImage.size.width
let imageX = myButton.minX -
((myView.myImageView.bounds.width)/2) + 15
let imageY = currentCellRect.minY -
(myView.myImageView.bounds.height) + 15
let imageFrame = CGRect(x: imageX, y: imageY, width:
imageWidth, height: imageHeight)
//Assign calculated from to the tool tip view
toolTipView.frame = imageFrame

After calculating imageWidth and imageX you could make a check like this:
[...]
var imageX = myButton.minX -
((myView.myImageView.bounds.width)/2) + 15
imageX = min(imageX, view.frame.width - imageWidth)
[...]
Notice that i changed imageX from let to var.

Related

How to make a UIImageView have a square size based on a given bounds/container

So I am trying to create a simple UIImageView to make it have a square frame/size with CGSize. Based on a given bounds. So for example if the bounds container is the width & height of the screen then. The function should resize the UIImageView to fit like a perfect square base on those bounds on the screen.
Code:
let myImageView = UIImageView()
myImageView.frame.origin.y = (self.view?.frame.height)! * 0.0
myImageView.frame.origin.x = (self.view?.frame.width)! * 0.0
myImageView.backgroundColor = UIColor.blue
self.view?.insertSubview(myImageView, at: 0)
//("self.view" is the ViewController view that is the same size as the devices screen)
MakeSquare(view: myImageView, boundsOf: self.view)
func MakeSquare(view passedview: UIImageView, boundsOf container: UIView) {
let ratio = container.frame.size.width / container.frame.size.height
if container.frame.width > container.frame.height {
let newHeight = container.frame.width / ratio
passedview.frame.size = CGSize(width: container.frame.width, height: newHeight)
} else{
let newWidth = container.frame.height * ratio
passedview.frame.size = CGSize(width: newWidth, height: container.frame.height)
}
}
The problem is its giving me back the same bounds/size of the container & not changed
Note: I really have know idea how to pull this off, but wanted to see if its possible. My function comes from a question here. That takes a UIImage and resizes its parent view to make the picture square.
This should do it (and will centre the image view in the containing view):
func makeSquare(view passedView: UIImageView, boundsOf container: UIView) {
let minSize = min(container.bounds.maxX, container.bounds.maxY)
passedView.bounds = CGRect(x: container.bounds.midX - minSize / 2.0,
y: container.bounds.midY - minSize / 2.0,
width: minSize, height: minSize)
}

Limit image size to ScrollView, Swift

I have an image which is set inside a scroll view, though I have set the frame of the scrollView to fixed height and width as shown below, the image goes beyond the bounds (see below picture).
How can I limit the picture to fit inside the scrollView.
imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight-50)
imageScrollView.clipsToBounds = true // Has no affect on the image
Do you have a reference to the UIImageView? If so, then set its content mode to aspect fit. Like this:
theImageView.contentMode = .scaleAspectFit
The clipsToBounds you set only covers up any parts of child views that are sticking out of the bounds of the parent view, so that's why it doesn't do anything for you.
OR if you're using Interface Builder, set this option:
So, what if you don't have the reference to the UIImageView?...
You could iterate through the subviews of your scroll view, and whenever it finds a UIImageView, you can set the content mode like that. Something like:
//This is off the top of my head, so my filtering may not be right...
//This is also a one and done solution if you've got a lot of images in your scroll view
for anImgVw in imageScrollView.subviews.filter({$0.isKind(of: UIImageView.self)})
{
anImgVw.contentMode = .scaleAspectFit
}
Otherwise, I'm not sure if it's possible without a reference to the UIImageView.
The library you are using is coded to match the scaling to the device orientation. So, if the image orientation doesn't match the view orientation, you end up with the image not quite fitting in your scroll view.
You'll need to edit the ImageScrollView.swift source file. Assuming you're using the same version that is currently at the link you provided ( https://github.com/huynguyencong/ImageScrollView ), change the setMaxMinZoomScalesForCurrentBounds() function as follows:
fileprivate func setMaxMinZoomScalesForCurrentBounds() {
// calculate min/max zoomscale
let xScale = bounds.width / imageSize.width // the scale needed to perfectly fit the image width-wise
let yScale = bounds.height / imageSize.height // the scale needed to perfectly fit the image height-wise
// fill width if the image and phone are both portrait or both landscape; otherwise take smaller scale
//let imagePortrait = imageSize.height > imageSize.width
//let phonePortrait = bounds.height >= bounds.width
//var minScale = (imagePortrait == phonePortrait) ? xScale : min(xScale, yScale)
//
// just take the min scale, so the image will completely fit regardless of orientation
var minScale = min(xScale, yScale)
let maxScale = maxScaleFromMinScale*minScale
// don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.)
if minScale > maxScale {
minScale = maxScale
}
maximumZoomScale = maxScale
minimumZoomScale = minScale * 0.999 // the multiply factor to prevent user cannot scroll page while they use this control in UIPageViewController
}
you can use the screenHeight rather than the viewHeight
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: screenHeight-50)

UIScrollView has large contentSize but doesn't scroll - no autoLayout

In my Swift 2 iOS 9.0 project I've set up a UIScrollView with a UIImageView and some buttons and labels. For some other reasons the scrollView's frame is 652 points wide and 652 points high. Its content is decided by the image in the imageView (it's 1000x1000):
func loadImageOntoView(FloorImage:UIImage) -> Void {
mapImageView.image = FloorImage
// containerView is my root view, same size as the screen
let cw = containerView.frame.width
let ch = containerView.frame.height
let cd = sqrt(cw*cw + ch*ch)
/* Here I set scrollView's frame to be a square with sides as
long as containerView's diagonal. This makes sure that if the scrollView
is rotated it's always bigger than the screen*/
scrollView.frame = CGRect(
x: -(cd-cw)/2, y: -(cd-ch)/2,
width: cd, height: cd
)
scrollView.contentSize = FloorImage.size
//Here it centers the imageView in the scrollView
mapImageView.center = CGPoint(
x: scrollView.frame.size.width/2, y: scrollView.frame.size.height/2)
scrollView.minimumZoomScale = 0.9
scrollView.maximumZoomScale = 15.1
}
I'm not completely sure on these scrollViews, but I thought it was supposed to be able to scroll with these settings? When I zoom in I can scroll, but when the zoomScale = 1 it doesn't work. What have I done wrong, or how should I go about using scrollViews?

How to move UIButton programmatically?

I would like to move one UIButton from:
to the bottom of the screen programmatically, how can I do that?
Your question isn't very clear, but you can get information about the screen size as follows:
// Screen Sizes
let screenWidth = UIScreen.mainScreen().bounds.width
let screenHeight = UIScreen.mainScreen().bounds.height
You can then modify the frame or the center position of the button like this:
// Change frame
btnName.frame = CGRectMake(screenWidth/3, screenHeight/2, 100, 100)
// Change center of button
btnName.center = CGPoint(x: screenHeight/2, y: screenWidth/2)
Hope it helps

UIImageView in ScrollView programmatically

I'm trying add UIImageViews to the ScrollView to get horizontaly pagging effect.
let pagesScrollViewSize = scrollView.frame.size
scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageImages.count), pagesScrollViewSize.height)
for var index = 0; index < 3; ++index {
var frame = containerView.bounds
frame.origin.x = frame.size.width * CGFloat(index)
frame.origin.y = 0.0
let newPageView = UIImageView(image: pageImages[index])
newPageView.frame = frame
newPageView.contentMode = .ScaleAspectFit
scrollView.addSubview(newPageView)
}
Here is my layout:
But on phone/emulator image is scaled to ScrollView height. I want scale image to scrollview width, so on one page will be one image.
To scale image to scrollview width you need to resize (scale image to width) + crop image first. Setting content mode to ScaleAspectFit will only "...scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent".
The problem was that I was using autolayout in my Storyboard but I was adding new view's without adding constrains. So I've added this photos manualy in storyboard like was shown here: UIScrollView Paging Autolayout & Storyboard

Resources