I have a UIView called descriptionView and I want to hide it initially when the screen first loads by offsetting the y coordinate to be the screen size + the height of descriptionView itself:
However, in my controller, none of my frame changes do anything:
override func viewDidLoad() {
super.viewDidLoad()
...
// descriptionView.frame.origin.y = self.view.frame.height
// UIView.animateWithDuration(1, animations: {
// self.descriptionView.frame.origin.y = self.view.frame.height
// self.view.layoutIfNeeded()
// })
//
print("xxx")
descriptionView.frame = CGRectMake(0, self.view.frame.height, self.view.frame.width, 66)
// descriptionView.frame = CGRectOffset(descriptionView.frame, 0, descriptionView.frame.height)
}
No matter what I do it seems fixed at that visible position like in my storyboard. Can someone help?
In IB you are using NSAutoLayout, so you either need to manipulate the constraints, or tell the view to translate the mask to constraints.
If you want to set the frame directly then you will want to do this:
descriptionView.translatesAutoresizingMaskIntoConstraints = true
descriptionView.frame = CGRectMake(...)
Otherwise you can create IBOutlets to the height and width constraint from IB and update those:
self.descriptionViewHeight.constant = self.view.frame.width
Additionally, I would recommend doing frame manipulations inside of viewWillAppear: rather than viewDidLoad. viewDidLoad does not strictly guarantee final position.
Instead of editing the frame or descriptionView, edit its height constraint.
First, create an NSLayoutConstraint from this constraint by cmd-dragging the height constraint from the Interface Builder to your class (like you do for any UI object).
Then you can set the constant property of this constraint to 0.
(Yes, constant is declared as a varproperty...)
If you are using constraints, chaging the frame view myView.frame property will not affect on view actual position and size. Instead of this make constraint outlet and change it in your code, it will look like this:
descriptionView.heightConstraint.constant = 100
Also, if you want to hide something, you can use property myView.hidden = true, or myView.alpha = 0;
Related
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 went through many threads here and tried two most recommended solutions.
Inside ViewDidLoad() method:
self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height * 0.254)
or
self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 0.254)
Also, in my storyboard I set low priority of the view's height constraint
(If I don't set height in storyboard, xcode would complain about ambiguous layout)
But none of these lines of code above does anything to darkBackgroundWithButtons, it remains the same height for each device size
This probably is the problem:
In interface builder you set constrains to your button, and therefore it doesn't change its height when you try to update the frame. Do this instead:
First connect your constrain from interface builder to your viewcontroller, just how you would normally do it with a button.
Then use this code to change the constrain:
var index = self.darkBackgroundWihButtons.constraints.indexOf("your constrain name")!
self.darkBackgroundWithButtons.constraints[index].constant = 0.2 // or whatever number you want
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;
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...
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
}
}