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

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))

Related

Text of textview is overlapping under textview

My question is simple and I think it is possible but I cant find it.
My design is like this
I give corner radius to textview and also set textContainerInset for padding from right and left both side
txtview_msg.textContainerInset = UIEdgeInsetsMake(5, 10, 10, 5)
Now my problem is like for first 2 line and last 2 line my text goes beneath textview so I need some solution for it.
Please help me into this. Thank you
try this code
UIView *paddingView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, txtview_msg.frame.size.height - 30)];
txtview_msg.leftView = paddingView1;
txtview_msg.leftViewMode = UITextFieldViewModeAlways;
UIView *paddingView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, txtview_msg.frame.size.height - 30)];
txtview_msg.leftView = paddingView2;
txtview_msg.rightViewMode = UITextFieldViewModeAlways;
You can create at first a view of same size how much you given, put cornerRadius how much you given in textview now. than create textview viewSize - corner radius. place the textview exactly in middle of view. set the constraints from that view only, than it will work perfect how you want.
check this attached screenshot:

Swift - Issues with setting background image in a UIScrollView

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

how to enlarge an image view within the view which is animating

I am trying to enlarge the size of an UIImageView which is inside a view.
Now the problem is I am showing this view with animation and it's becoming larger from small in size however the image is not enlarging. To make this UIImageView larger I am writing the same code which I wrote for view; that is
self.alertView.frame = CGRectMake(30, 100, 235, 190);
self.alertImgView.frame = CGRectMake(20, 20, 150, 150);
and also i want to show this image view in between of this view that is not happening either. Please suggest
Have you tried this :-
[self.alertImgView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight)];

Programmatically create subview that covers the top 50% of the iPhone screen

I have a subview and I have been customizing its size by using the frame property and setting its value to the CGRectMake function's parameter values.
I have slowly but surely been changing the CGRectMake parameters and re-running the app to get the subview to the correct position on the screen but I know there has to be an easier way.
Here is what I am currently doing:
UIImageView *halfView = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:halfView];
halfView.frame = CGRectMake(0, 0, 320, 270);
Is there a way that I can stop having to manually enter those 4 parameters into CGRectMake, and just set it to the top 50% of the screen?
Here is what I want the subview to look like on the iphone's screen:
halfView.frame = CGRectMake(0, 0, 320, self.view.bounds.size.height/2);
you just take the height and divide it by two
You should also probably change 320 to self.view.bounds.size.width
I suggest reading this post to really get a grasp of UIViews and working with them:
UIView frame, bounds and center

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.

Resources