Change imageview height programmatically ios - ios

I have this kind of structure in storyboard:
UIViewController > View > Scrollview > Scrollview > UIImageview
By Default imageview size is : 320x230. Check below image:
Now i need to change size of that imageview Programmatically.
Lets say i need to set frame : 320x280
Autolayout is disabled see image:
Here is what i have done:
override func viewDidLayoutSubviews(){
super.viewDidLayoutSubviews()
imgThumbnail.frame = (frame: CGRectMake(0, 0, self.result.width, 280))
}
But its not working at all. Its not changing size of imageview. One more important thing is when i scroll down & up the scrollview. It takes the new frame which i have set in above code in imageview.
It will be great if someone guide me resolve this issue.
Thanks in advance.

Set imageView Autoresizing Property to None and height to required height as follows:-
imageView.autoresizingMask = .None
imageView.frame.size.height = 280.0
To better understand Autoresizing Masks refer this link :-
Autoresizing masks programmatically vs Interface Builder / xib / nib

Related

Swift programmatically place UIImageView on top of another and resize

On my storyboard I have 2 image views.
One holds the main image, the second is an overlay which will be used to specify the crop area of the main image.
In my viewDidLoad I've done this.
let screen_width = UIScreen.mainScreen().bounds.width
let screen_height = UIScreen.mainScreen().bounds.height
self.overlayImage.frame = CGRect(x: self.imageView.frame.origin.x, y: self.imageView.frame.origin.y, width: screen_width, height: (screen_height * 0.1919))
The goal is to have the overlayImage's top left corner lined up properly with the imageView's top left corner. Also the height of the overlay should be about 1/5th of the screens size. However when I run the code the overlayImage is exactly the same size and in the same location it was originally on the storyboard.
How can I programmatically line it up on top of the imageView after the image has been set to it, and also resize the overlayImage dynamically in the viewDidload?
I'd just do it manually in storyboard editor but everyone will have different screen sizes so I thought it best to use the mainscreen().bounds.height variable to determine the amount of height to use dynamically at runtime.
All you have to do is to write your above code in viewDidAppear instead of viewDidLoad as below:-
override func viewDidAppear(animated: Bool) {
let screen_width = UIScreen.mainScreen().bounds.size.width
let screen_height = UIScreen.mainScreen().bounds.size.height
self.overlayImage.frame = CGRect(x: 0, y: 0, width: screen_width, height:screen_height/5)
}
And your frame will set itself as desired.
Since you're working with Storyboard, I would rather use Autolayout and Constraints to solve this problem for me... For instance you set the constraints for your first image view, then set the constraints of your overlay based and related to the former one. Example:
Lower UIImageView: set leading, trailing, top and bottom constraints
Overlay: set center X and Y constraints to be equal to the lower Image
Overlay: set height and width to be proportional to the lower image
Thus, when ever the screen size changes, the two images will always resize equally...
Interface builder:
Set overlay top, leading, width to your image view.
give your overlay a constant height constraint; the constraint cosntant value is arbitrary because you will replace it at run time
ctrl + drag an IBOutlet from this height constraint to your viewcontroller
in your viewWillLayoutSubViews set the .constant of your height constraint to UIScreen.main.bounds.size.height / 5

How to set full width image inside UIScrollView in swift

I need to display large content in my UIViewController added in Storyboard so I added UIScrollView with constraints top, right, bottom, left:0 ( to make it full screen ).
In top of UIScrollView I need a square image with its width as device screen size, so I added UIImageView with constraints : aspect ratio->1:1, top:0, centre align in container, height : 100.
And below It there is a UILabel where I want to show large text. I am using Auto Layout.
Is there an option to do this in Storyboard ?
In swift I tried below
Connected Image in controller file as :
#IBOutlet weak var eventThumb: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// set image path from url ,it works
eventThumb.image = UIImage(data: NSData(contentsOfURL: NSURL(string: self.event!.image)!)!)
let screenSize: CGRect = UIScreen.mainScreen().bounds
eventThumb.frame = CGRectMake( 0, 0, screenSize.width, screenSize.width)
}
I tried related answers given here ,here and here but they not seem to working in my case.
I am new in swift and ios, am I doing something wrong in structure ?
Edit :
Image added
Call it in:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// your layout code goes here, AFTER the call to super
}
You forgot to call super before your code.
You had tried to make the frame with following screen size
eventThumb.frame = CGRectMake( 0, 0, screenSize.width, screenSize.width)
Please crosscheck it with
eventThumb.frame = CGRectMake( 0, 0, screenSize.width, screenSize.height)
Simply Try this Steps:
First Remove all red lines here you can see in Image
Now keep that line as it is, where you are setting a frame of Imageview
Note:
I already tried this solution in swift & worked for me. I hope it will work for you.
Edited:
As you mentioned you are using Auto layout, So no need to disable it. Just do 1 thing
Put the line which you are setting up the frame of image view in this method:
-(void)viewDidLayoutSubviews

UIView inside custom UITableViewCell wrong width / height

I ran into the following problem. I have a custom class UITableViewCell with a few UIViews inside, which act as container for chartviews.
The problem is if I do the following:
self.upperLeftChart = XYPieChart(frame: CGRectMake(0, 0, self.upperLeftContainer.frame.width, self.upperLeftContainer.frame.height))
self.upperLeftContainer.addSubview(self.upperLeftChart)
The Chartview is as big as the whole UITableViewCell instead of the size of my container.
If I print the size of my container
NSLog("\(self.upperLeftContainer.frame.width) / \(self.upperLeftContainer.frame.height)")
it prints: 320.0 / 568.0 which is wrong. My container is about a quarter of the whole cell.
I guess it hast something to do with my Autolayout + Constraints. If I set the size to 120x120px hardcoded, it works nicely.
Any trick to get the real width and height of UIView arranged with autolayout and constraints?
What you need to do is to override
override func layoutSubviews() {
super.layoutSubviews()
self.upperLeftCart.frame = CGRectMake(0, 0, self.upperLeftContainer.frame.width, self.upperLeftContainer.frame.height)
}
That way it will change frame of the view every time cell resizes

swift - I'm dumb and my subview wont obey and correctly size itself

I feel like this is a softball for you veterans, but I'm having a lot of trouble resizing the subview of my UIImageView... here is what I got:
var myImageView:UIImageView!
var tmpView:UIImageView!
myImageView is my "main" UIImageView, and I'm treating tmpView as a subview... now I've tried both with and without auto layout on, but I've set the constraints of myImageView s.t. myImageView takes up the whole screen. I've confirmed this to be true by setting myImageView.image = UIImage...etc.
Here is my code for initializing the image and adding it to the subview:
self.tmpView.image = UIImage(named: "myImage.png")
self.tmpView.frame = CGRect(x: 0, y: 0, width: 200, height: 300)
self.imageView2.addSubview(self.tmpView)
Now here is where I am running into problems - no matter what I set tmpView's height and width to, the size never changes. Interestingly though, changing x,y does have an effect on the position of the subview.
Here are my questions: 1) Why do both x and y obey nicely, but width and height do not?
2) How do I fix this, and is it best to do so programmatically or through the storyboard?
You should no longer modify the frames directly when using autolayout. Add a width and height constraint to tmpView, and create outlets for them if you're using IB (The outlets should have the type NSLayoutContraint).
You can also create the constraints in code but there's no point of doing it that way unless you have to.
In both cases, to resize tmpView, change the constant property of each of the constraints to the values you want.
widthConstraint.constant = yourNewWidth;
heightConstraint.constant = yourNewHeight;

Programmatically set size of UIView from Storyboard

I have placed UIView in UIScrollView in Storyboard. The UIView is pinned to the all the sides of the UIScrollView. I am trying to set the size of UIView programatically because it is needed for scrolling to work properly. How can I programmatically set the width and height of the UIView?
Thanks for any help as I am trying to solve the problem already for a week...
You'll need to change the frame of your view as below:
yourView.frame = CGRectMake(x,y,calculatedWidth,calculatedHeight);
Plus, you'll also need to check for the scroll view's frame so that it enlarges as per your view's width and height.
You need to set new frame to change width, height, x or y for example:
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, newWidth, newHeight);

Resources