I create a UIView according its superview's frame. Its superview's frame is always portrait one (568, 1024) actually this application only support for iPad's landscape left and right. How could get a landscape frame when init a UIView?
Related
I’m building a custom keyboard I’m having a hard time figuring out how to calculate the width of the screen.
What I’m doing:
I calculate the dimensions of my keyboard in a function where I check whether the device is in portrait or landscape mode:
let screen = UIScreen.main.bounds
if screen.width < screen.height { //Portrait
} else{ //Landscape
}
When the device is in portrait I calculate the buttons width dividing for the width of the screen.
When the device is in landscape I calculate the buttons dimensions dividing for the HEIGHT of the screen because when rotating the screen it considers the height to be the keyboard width.
Then in my UIInputViewController I use the function
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
}
to update the constraints and the dimensions when I rotate the screen.
By logic I’d check for the device’s width and divide it to get the buttons width. The problem is that if I do that, there are issues when I rotate the screen:
If I open the keyboard on landscape mode, the width of the buttons is incorrect because the screen height is considered to be the portrait device width (as I stated above).
If I use view.frame.width the width I get is 0 unless I call it in viewDidLayoutSubviews. But I need to call it elsewhere.
If I open the keyboard and it is in portrait mode and then I turn the device in landscape mode everything works as expected.
So is there a way to always get the right and current keyboard width in a custom keyboard?
I have a UIview with some subviews in it! When I transform that view (scale, rotate.. use CATransform3D). These subviews grow bigger or smaller but the subviews do not change the frame. How can I detect the real frame of them? Thanks!
viewTransfromContainer?.layer.transform = CATransform3DScale((viewTransfromContainer?.layer.transform)!, pinchGes.scale, pinchGes.scale, pinchGes.scale)
I used convert(<#T##rect: CGRect##CGRect#>, to: <#T##UIView?#>) It's work fine with scale But Not return true value when rotate.
image bellow
I'm looking for a way to move a UIView offscreen with a swipe gesture. I got everything set up, but noticed that the code below moves the panel down, rather than offscreen in portrait mode. Is there a way for me to specify "Move UIView to the right by x pixels, regardless of device orientation or autolayout? Maybe dynamically changing the "leading space to superview" is the answer?
CGPoint sidePanelCenter = self.sidePanel.center;
float width = self.sidePanel.bounds.size.width;
self.sidePanel.center = CGPointMake(sidePanelCenter.x-width, sidePanelCenter.y);
Set the NSLayoutConstraint that sets the UIView position as an IBOutlet and change it in code instead of the center property.
I have an image as a subview of a UIScrollView. I want the image to initially fit the screen's bounds using autoResizingMask and contentMode (UIViewContentModeAspectFit) property of UIView. When the scrollview's frame is changed to make room for the keyboard, I don't want the child image view to scale down to fit the smaller frame. I can't disable autoResizeSubviews on the scroll view when it is created because the child view must be re-sized once at the beginning. Right now I can turn off subview re-sizing when the keyboard appears and re-enable it when it dissapears. This seems to work fine but seems hackish. Is that an acceptable way to do it or is there a better solution? Thanks.
Don't worry about the autoResizingMask since it's a subview of the UIScrollView.
The autoResizingMask on a UIView allows for the view to be automatically resized when its parent view's frame has been resized. In this scenario, it sounds like your scrollView's frame is being adjusted vertically to accommodate the keyboard's on screen frame. When the parent view's frame shrinks, so does your UIImageView, which means it works as designed if you're using UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight as the autoResizingMask of your UIImageView.
Instead of using autoResizingMask on your UIImageView, you should manually set its frame to be the same size as the UIScrollView's frame via the bounds property:
imageView.frame = scrollView.bounds;
Then let's set the scrollView's contentSize to be the same size as the imageView.
scrollView.contentSize = imageView.frame.size;
This way, your imageView should be the full size of your scrollView, and won't move if you adjust the size of the scrollView frame.
I have a UITableViewController with a custom UIView as a subview. When I rotate the iPhone from portrait to landscape orientation the UIView subview rotates correctly but does not resize correctly. How can I resize the custom UIView when the device is rotated?
Set the UIView's autoresizingMask like so:
myView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
This should mean that the view resizes itself automatically on rotation.