I would like to rescale my image to fit the width of an iOS screen. The following is the code that I used.
#IBOutlet var bgImageView: UIImageView
override func viewDidLoad() {
super.viewDidLoad()
bgImageView.contentMode = UIViewContentMode.ScaleAspectFit;
bgImageView.frame.size.width = UIScreen.mainScreen().bounds.width;
}
This solution however seems to be causing thread problems. What's wrong here?
You can set your image view constrains as follows - its width should be equal to your super view width, centerX and centerY are the same as super's. And height not an equal constraint, but lessThanOrEqual to super's height.
And yes, it is useless to set frame manually while using auto layout constraints
Related
The Question
how can I keep Buttons, Images, Buttons...Views rounded in big screens when using constraints of ( buttons, images....views ) equal width and height to superview using swift
The Code I've Tried
I tried this block of code and It works fine in small screens like SE..until 8 :
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
roundedObject.layer.cornerRadius = roundedObject.frame.width / 2
roundedObject.clipsToBounds = true
}
the shape of the different sizes screens
my object constraints:
screen1 screen2 screen3
the issue is your constraint
you will not get a square by using proportional width and proportional height constraint together like that, because different device will have different height and width
my suggestion is to use only one proportional width or proportional height (which one you want) and use aspect ratio constraint 1:1 on the roundedObject
Probably you're doing that in viewDidLoad method where the frames didn't get the final value yet (initially they get the values from the storyboard from where they were instantiated. So if in the storyboard preview the size is f.e. 300x300, in the viewDidLoad it also will be 300x300).
So either do that in the viewDidLayoutSubviews or create a subclass and do that in the layoutSubviews.
class RoundedButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
layer.corderRadius = bounds.width / 2
layer.masksToBounds = true
}
}
In my project I am using AutoLayOut, There is one UIView which is subView main View, I have set its width(using constrains) equal to 2 :3 superView's width and its height equal to its View. I need view to be circular shape so I am setting its cornerRadius to heightOfView/2. There is 1:1 aspect ration of views's height and width. I have created Outlet of that constrain as
#IBOutlet weak var circleHeight: NSLayoutConstraint
Now I want to acces its Height or Width but I m not gwtting actual value,
circleHeight.firstItem.frame.width
I am getting value which I have stored from Storyboard, There Is something I am Missing but could not figured it out
Selected View should be circular. but when i print its constain's values it gives height and width as 214 only
You can create IBOutlet for this UIView, which should be circle shape.
Then in viewDidLayoutSubviews() method you can read its height and make it circle:
override func viewDidLayoutSubviews() {
let viewHeight = CGRectGetHeight(yourView.frame)
yourView.layer.cornerRadius = viewHeight / 2
yourView.layer.masksToBounds = true
}
I think you might be fetching value in viewWillAppear or
ViewDidLoad but views frames don't get updated when these method gets
called
So you need to fetch the width or height in ViewDidAppear method.
If get the height or width in ViewDidLayoutSubViews, performance
gets degraded as it gets called many times.
Here is Code
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
let viewHeight = CGRectGetHeight(yourView.frame)
yourView.layer.cornerRadius = viewHeight / 2
yourView.layer.masksToBounds = true
}
A constraint does not have height or width, only relationships, priorities and constant values, depending in how you set the constraint itself.
To access the height of the subview you are interested in, you need to access as always:
<uiview_outlet>.frame.size.height
Hope it helps.
I have an image inside of a UIScrollView. The Image's length is much larger then the device screen size and that is why i placed it in a scrollview. However, to avoid clipping and distortion of the image I have the constraints set so the image will always have the correct aspect ratio. When you run this on different size screens, the image is different sizes. Now I want to be able to have the scrollview be the same size as my image so that the scrollview is never bigger then the image itself. Is there some code I can program in to make my scrollview always the same height as my image? I need my scrollview to change depending on the height of the image since on different devices the height of my image will change.
so far I have:
class Second : UIViewController {
#IBOutlet weak var myScrollView: UIScrollView!
#IBOutlet weak var myImage: UIImageView!
override func viewDidLoad() {
ScrollView.contentSize.height = 1300
}
The constraints I used on the image are:
Equal width as the scrollview
entered horizontally in the scrollview
Top space to the top of the scrollview
Aspect fit (so the image is not distorted)
Thank you. Any help is very appreciated!
Try this, override your 'viewDidLayoutSubviews' method and you shall be able to get the frame of the image within that method (you will not get the frame of the image in your viewDidLoad method, see reference link below). From the frame, get the height of the image, and then set the content size for the scrollview as pointed by Victor above. Let me know if it works.
See this question for reference: iOS AutoLayout - get frame size width
You can set the UIScrollView contentSize property in your viewDidLoad like the following way:
override func viewDidLoad() {
myScrollView.contentSize = CGSize(width: widthYouWantToSet, height: heightYouWantToSet)
}
But, be careful in you can experience rotations in your app, this need to be updated if the height change.
I hope this help you.
Should be,
override func viewDidLoad() {
myScrollView.contentSize.height = 1300
}
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...
I have a Storyboard that uses Auto Layout & Size Classes. I've given up trying to get my UIView to use the Aspect Ratio 1:1 constraint to work. It takes over the entire screen rather than just set height = width like a UIImageViewer does.
So I am trying to change the width and height constraints instead. In IB the width constraint is set to 320 and the height constraint is set to 320. Ultimately I will set the width constraint to the width of the device and the height constraint to the width of the device, hopefully getting a square UIView for the camera to display its session.
However, when I set the constants and then setNeedsUpdateContraints, nothing happens. Yes I would prefer not to do it this way, but I would also prefer to get this &^&$^#( thing to work and not spend any more hours fighting it. Setting leading space to 0, trailing space to 0, top space to 0, and aspect ratio 1:1 does not work. Even though I did it with a UIImageView and it does work for that.
#IBOutlet var captureFrame: UIView!
#IBOutlet var viewHeightConstraint: NSLayoutConstraint!
#IBOutlet var viewWidthConstraint: NSLayoutConstraint!
override func viewDidAppear(animated: Bool) {
self.viewWidthConstraint.constant = 100 //100 = temporary
self.viewHeightConstraint.constant = 100 //100 = temporary
captureFrame.setNeedsUpdateConstraints()
captureFrame.layoutIfNeeded()
}
Any idea how to get a square UIView so that the camera can be displayed in it using IB, auto layout, size classes AND not have it zoomed in X2?
Why aren't my constraints adjusting to 100? Why is my UIView still 320 x 320?