IOS self.view.frame.width equals 0 - ios

I am making a custom keyboard. In order to determine the orientation of some buttons I need to find the width of the keyboard. I do this by using:
self.view.frame.width
in my viewDidLoad() method.
However, it returns 0.

this can happen in two scenarios
1- you didn't set the size of the self.view (somehow ... if you created that view programmatically..)
2- if you didn't initialize that view at all ...
However if the keyboard width equals the viewcontroller width , you can use this code below:
CGFloat width = [UIScreen mainScreen].bounds.size.width;

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

Swift - setting UIView's size based on size screen

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

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

Not getting a correct height for bounds of iphone screen

I am having trouble setting up the frame for one of my views. I know that the height in points of the iPhone 5 is 568, but when I enter this code into the viewDidLoad in my ViewController:
var height = self.bounds.height
println(height)
output:
640.0
Why does it come out as this when it should be 568? Is it a problem with the simulator?
I thin your viewController height is more than 568.You have did FreeForm.Check the height in Size inspector of your view controller.
Make simulated size fixed.
EDIT: You can make it portrait and iPhone 4 inch to show it correctly see below.
To get the height of the screen you want the height of the VIEW. To do that you would want:
DO NOT PUT:
var height = self.view.bounds.height
DO:
var height = self.view.bounds.size.height

Container UIView alignment to the bottom

I'm trying to aligning a container UIView to the bottom of the screen in such a way it always is at the bottom of any device whatever the resolution or height is but I can't figure out how to do this via the storyboard.
Any insight is appreciated. Thanks a lot!!!
You have to make a vertical constraint between your view and its container view with a constant of your desired space between the views.
One way via IB is to CTRL-Drag from your view to the container view and select bottom space to bottom layout guide.
Make sure to check the constant after creating the layout constraint to have the correct value since ib will use the current distance for the two views.
Use autolayout (official documentation), or you can do this:
// Assuming you want to move a UIView *foo
CGFloat screenHeight = [[[[UIScreen mainScreen] bounds] size] height];
CGRect newFrame = foo.frame;
newFrame.origin.y = screenHeight - foo.frame.size.height;
foo.frame = newFrame;
With autolayout:
In this picture, in the top most part of the pop up dialog, the drop down menus with the numbers will let you choose what to set the constraint relative too. Try this.

Resources