How to set full width image inside UIScrollView in swift - ios

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

Related

Swift 3 Can't touch UIButton on the bottom of UIScroll

I have a problem with Xcode 8 and Swift 3 UIButton inside UI ScrollView which the UIButton can't touch/tap. The structure of storyboard like this.
View
-Scroll View
--Content View
---Content Label (dynamic long content get from API)
---Agree Button
And this lines of codes for making scroll working with the dynamic label.
override func viewDidLayoutSubviews() {
let maxLabelWidth: CGFloat = self.contentView.frame.size.width
let neededSize = self.contentLabel.sizeThatFits(CGSize(width: maxLabelWidth, height: CGFloat.greatestFiniteMagnitude))
self.contentView.frame.size.height = neededSize.height+self.agreeButton.frame.size.height+300
self.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.contentView.frame.size.height)
}
Everything is setup as default in the storyboard. Is there another configuration to solve this problem?
It is highly likely that your UIButton is not within the bounds of its superview. That is usually the problem when I see this happen. Try setting the background color of the UIButton's superview to red or something and see if the button is within the red area.

Change imageview height programmatically 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

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;

How to setup a view with a half of screen height?

I want to set view1's height to half of the screen's height for all devices (when the device in the portrait mode)
Here is what I want it to look like:
so I make an auto layout of View1's height
#IBOutlet weak var heighConstraint: NSLayoutConstraint!
my viewwillappear function here:
override func viewWillAppear(animated: Bool) {
self. heighConstraint.constant = self.view.frame.size.height / 2
}
But it's not worked when I ran my app. what's wrong in here?
I know the accepted answer is correct but there is simpler method to do so -
Add the view (the one shown in yellow color). Pin it to three edges (top, leading and trailing). I have set it as 0 but change it as per your need.
Create Height Equal to constraint to main View as
Open that Constraint in the Inspector view and edit the multiplier as 0.5.
Setting it as 0.5 takes the height value of half the height of mainView.
Just make sure FirstItem = View1 and SecondItem = SuperView as shown in the image.
Try to change heightConstraint's constant value in viewDidLayoutSubviews method.
override func viewDidLayoutSubviews() {
heightConstraint.constant = self.view.frame.size.height / 2
}
Please try this out. Hope it will help you out. The below line will take the bounds of the device and will set the frame or height according to it.
view1.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/2);
Hope this helps!!! Good Luck...

How to get height of topLayoutGuide?

moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 0, y:{layoutguide.height}, width:
self.view.frame.width, height: 300)
self.view.addSubview(moviePlayer.view)
viewcontroller have an attribute named "topLayoutGuide", but it seems not I wanted.
I know how to implement in storyboard, in code, I can't get the height of Top Layout Guide. I searched for hours getting nothing.
Below is wanted.
You can get the topLayoutGuide value as its length property:
// Inside your viewController
self.topLayoutGuide.length
Since it is a single value (i.e.: it does not have a height and width) they just called it length. Same holds for bottomLayoutGuide.
Hope this helps
One more thing to mention, in the apple doc for this property:
// As a courtesy when not using auto layout, this value is safe to refer to in -viewDidLayoutSubviews, or in -layoutSubviews after calling super
Use this property in these two functions will get you the accurate value, since the layout has been initialized. If you use it in the viewDidLoad function, this property will be 0.
If someone is looking for how to calculate the height of insets of SafeLayoutGuide available for iOS 11 because Top and Bottom Layout Guide are deprecated now, you can find it in:
view.safeAreaInsets
Notice that Top and Bottom Layout Guide were a part of ViewController, and now SafeLayoutGuide is a part of the main view of the ViewController.
This gets the length (in points) of the portion of a view controller's view that is overlaid by translucent or transparent UIKit bars.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let topSpace:CGFloat
let bottomSpace:CGFloat
if #available(iOS 11.0, *) {
topSpace = self.view.safeAreaInsets.top
bottomSpace = self.view.safeAreaInsets.bottom
} else {
topSpace = self.topLayoutGuide.length
bottomSpace = self.bottomLayoutGuide.length
}
}

Resources