Swift - Issues with setting background image in a UIScrollView - ios

I have created a UIScrollView and added UILabels and UIButtons. I then have created a UIImageView and added it to the my UIScrollView. I have used the following code to set the UIImageView to the background of my UIScrollView.
let paperImageView = UIImageView(image: UIImage(named: "White Notebook Paper.png"))
paperImageView.frame = CGRect(x: -60, y: -60, width: UIScreen.main.bounds.width + 60, height: currentY + 60)
scrollView.addSubview(paperImageView)
scrollView.sendSubview(toBack: paperImageView)
As you can see in the following screenshot, by UIImageView is distorted.
The background image supposed to look like notebook paper. As you can see, the spacing of the lines becomes wider the lower you go in the UIScrollView. Thanks for taking the time to share your thoughts.

I forgot to change the content mode of the image view.
paperImageView.contentMode = .scaleAspectFill

Related

UIImageView disappears when enabling "ClipToBounds"

I have an UIImageView element that I added via Interface Builder (XIB file).
I create an IBOutlet to this imageView and on awakeFromNib() method I add those 2 lines (for creating a circular imageView):
self.myImageView.layer.cornerRadius = self.myImageView.frame.size.width / 2
self.myImageView.clipsToBounds = true
The constraints to this UIImageView (via Interface Builder, NameLabel is a UIlabel with frame of: x: 27, y: 170, width: 319, height: 25) are:
Width = 77
Top space to SuperView = 88
Bottom space to NameLabel = 5
Align center X = NameLabel
When I run the app I can't see the imageView - it just disappears!
Note: (I think it happened every time a constraint sets its height).
Note 2: When I remove self.myImageView.clipsToBounds = true line or self.myImageView.layer.cornerRadius = self.myImageView.frame.size.width / 2 line it works!
Any idea how to fix it?
Thank you!

What size should I have my navigationItem.titleView set to?

I've been trying to put an image in my titleView in the navigation bar. The image keeps coming out way too blurry and I can't seem to understand why for the life of me.
Right now my images are 28x14 for the #1x, 56x28 for the #2x, 84x42 #3x and the frame of the titleView is 50x50. Should I be sizing the #1x 50px, #2x 100px and the #3x at 150px? Essentially, does the width and height of the image rely on the frame?
I'm not entirely sure how to go about this, any help would be appreciated. I am trying to have the image work for all iPhone sizes, 5,6 and 6 plus.
Code is below:
let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
imageView.image = UIImage(named: "NavigationTitleLogo")
self.navigationItem.titleView = imageView
Addition
I'm also having the same issue with my MKAnnotationPin images.

How to programatically adjust x and y axis of image in Swift?

I am trying to centre my round image in the middle of my screen. (I have a video playing underneath it). I cannot seem to get this to centre based on the screen size. I am sure it is a simple solution , but if anyone has any ideas I would be very grateful
Here is the image of my simulator:
And here is my code:
imageViewObject = UIImageView (frame: CGRectMake(CGRectGetMidX(self.view.frame), 150, 150, 150))
imageViewObject.image = UIImage(named:"one.png")
self.view.addSubview(imageViewObject)
Thank you in advance for any help!
The problem is that the position origin is the top left corner. You should use something like this instead:
imageViewObject = UIImageView (frame: CGRectMake(CGRectGetMidX(self.view.frame) - 150/2, 150, 150, 150))

Image ResizableImageWithCapInsets resizing Mode not stretching image uniformly

I have an image view of size
CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 30)
I have an image sized 50 x 50 Px. It contains an drawing at the centre of it. I wish to stretch this image to fit the imageView. I achieve this by the following
viewImage.contentMode = UIViewContentModeScaleAspectFit;
Then it stretches the image in all directions and looks ugly. Then I used the resizeImageWithCapInsets as the following
[placeHoldImage resizableImageWithCapInsets:UIEdgeInsetsMake(1.0, 1.0, 40.0, 40.0) resizingMode:UIImageResizingModeStretch]
However this produces the following image
What I wish to change in this image is that the house symbol at the right bottom corner to be centred. This is the drawing of the image (50 x 50) which I dint want to get stretched.

How to make UIImageView automatically resize to the size of the image loaded

I have put an UIImageView control on my view with IB.
The size of the control is just something I decided upon, pretty random size really
What I want to do is the control to resize automatically whenever I set the image property to a new image. I want it to actually resize to the size of the image.
Can it be done automatically ? without any code intervention ?
If not - what will the best approach be in this case ?
What happens today is strange. I load images into the ImageView and I see the images getting displayed properly even though the size of the ImageView is not changed. This interferes with my intention of grabbing users touches over the ImageView. The user touches the actual image, but since some parts of the image are outside ( and this is the strange part ) of the ImageView - point mapping goes crazy
Can someone think of any explanation to this ?
thanks
The size of an image has no bearing on how large the UIImageView actually is, rather the size of the UIImageView solely depends on the size given to it in Interface Builder (or that you assigned to it). Else the images would be all whacky when you use the #2x images for Retina displays for example.
If you want to fix this, you must change the frame when setting the image as well. If you're doing this now:
[imageView setImage:[UIImage imageNamed:#"myImage.jpg"]];
change it to:
UIImage img = [UIImage imageNamed:#"myImage.jpg"];
[imageView setImage:img];
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,
img.size.width, img.size.height);
This will however not change the layout of view it is contained within, you can make it change the sizes of the other views automatically under iOS 6 using Layout Constraints. If you are an Apple Developer you can watch the WWDC instruction videos, they explain how that system works quite well.
If you're fine with the view not growing, and the problem is just how the image overflows it's bounds when you change it to one that does not match the dimension of the containing view, you can set the "Clip Subviews" checkbox in Interface Builder for the image view. This will make it so that the view will not draw anything outside it's own bounding box, if you also set the scaling mode to "Aspect Fill" or "Scale To Fill", the image will always fill up the entire bounds of the containing view.
Here is the code snippet I cut and paste often, using the same method as Hampus Nilsson above -
Example
func demo(x0:CGFloat, y0:CGFloat) {
let imgView = UIImageView(image: UIImage(named: "someImg.png"));
imgView.sizeToImage();
imgView.center = CGPoint(x:x0, y:y0);
}
UIImageView Extension
extension UIImageView {
/******************************************************************************/
/** #fcn sizeToImage()
* #brief size view to image
* ##assum (image!=nil)
*/
/******************************************************************************/
func sizeToImage() {
//Grab loc
let xC = self.center.x;
let yC = self.center.y;
//Size to fit
self.frame = CGRect (x: 0, y: 0, width: (self.image?.size.width)!/2, height: (self.image?.size.height)!/2);
//Move to loc
self.center = CGPoint(x:xC, y:yC);
return;
}
A wonderful cut & paste, use if needed!
let containerView = UIView(frame: CGRect(x:0,y:0,width:320,height:500))
let imageView = UIImageView()
if let image = UIImage(named: "Image_Name_Here") {
let ratio = image.size.width / image.size.height
if containerView.frame.width > containerView.frame.height {
let newHeight = containerView.frame.width / ratio
imageView.frame.size = CGSize(width: containerView.frame.width, height: newHeight)
}
else{
let newWidth = containerView.frame.height * ratio
imageView.frame.size = CGSize(width: newWidth, height: containerView.frame.height)
}
}

Resources