iOS - View rotate and resize - ios

Here is myView
When
self.myView.transform = CGAffineTransformMakeRotation(M_PI*0/180);
[self.myView setFrame:CGRectMake:(self.myView.frame.orgin.x,self.myView.frame.orgin.y,300,self.myView.frame.size.height)];
it will be
now when i rotate the view
And do this again
self.myView.transform = CGAffineTransformMakeRotation(M_PI*0/180);
[self.myView setFrame:CGRectMake:(self.myView.frame.orgin.x,self.myView.frame.orgin.y,300,self.myView.frame.size.height)];
What happened here?
seen like the width is stretched.
Its anyway to fix it or have other way to rotate and resize?

You should use the bounds and center properties to resize and place your view, frame should not be used if the transform property is not the identity transform
(see warning at the UIView documentation for frame)
frame.size is not equal to bounds.size when your view is rotated, if you want your view to have a width of 300, you should set the bounds

Related

detect subview's frame when superview transform

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

UIView appears with wrong center position

In the following function I define all the necessary characteristics of a UIView:
func OpenController() {
var gameOverView: UIView = UIView()
gameOverView.center = super.view.center
gameOverView.frame.size = CGSize(width: 200, height: 300)
gameOverView.backgroundColor = UIColor.grayColor()
self.view.addSubview(gameOverView)
}
Even though I define the center of the UIView "gameOverView" as that of the the viewcontroller it resides in, it appears with a corner in the center of the viewcontroller and not centered in the viewcontroller. I have tried various other ways of defining the position (NSLayoutConstraints, frame.x, frame.y etc.) but all have this result.
If anyone can tell me why this happens and how to center the UIView within its parent view controller I would greatly appreciate it!
Your issue here is that your center is being set before the frame. Since you are creating the view without the frame argument your frame is {0, 0}.
So you are currently centering the subview then resizing it, so this is happening:
What you need to do is resize the subview then center it, like this:
So you can just swap your centering and framing logic:
gameOverView.frame.size = CGSize(width: 200, height: 300)
gameOverView.center = super.view.center
Otherwise even easier just pass the frame when creating the view (you could even pass in the proper x, y coordinates to center it here too):
var gameOverView: UIView = UIView(frame: CGRectMake(0, 0, 200, 300))
You have to set the center of the view after setting its size. This is because before setting the size or frame of a view its frame rect is (x:0,y:0,width:0,height:0). If you then immediately set its center, then a view of size zero will get centered so its new frame rect could be (x:20,y:20,width:0,height:0) (if the parent view is 40x40). If you now change the view's size, the origin point of the view will not actually move so the new frame could be (x:20,y:20,with:5,height:5) which means the view is no longer centered.
So to center a view you have to first set its size and the its center.

Programmatically set size of UIView from Storyboard

I have placed UIView in UIScrollView in Storyboard. The UIView is pinned to the all the sides of the UIScrollView. I am trying to set the size of UIView programatically because it is needed for scrolling to work properly. How can I programmatically set the width and height of the UIView?
Thanks for any help as I am trying to solve the problem already for a week...
You'll need to change the frame of your view as below:
yourView.frame = CGRectMake(x,y,calculatedWidth,calculatedHeight);
Plus, you'll also need to check for the scroll view's frame so that it enlarges as per your view's width and height.
You need to set new frame to change width, height, x or y for example:
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, newWidth, newHeight);

How to force UIView (mapView) to take up the entire screen

To set a UIView to take up the entire screen, is this correct?
self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
Or should I be using the self.view.bounds...?
You can use either of frame and bounds for view as frame and bounds are same for it. But when you work with subviews then use bounds because frame and bounds need not be the same. For subview they are same only when your subview's size is same as its superview.

Fix position of custom view in UIView

I have a custom view with some drawing in drawRect added as subview in a UIView.On tapping on the subview I change the height of the base view.This is done by -
CGRect rect = self.bounds;
rect.size.height = 400.0;
self.bounds = rect;
This also repositions the custom view (sets it to the top of the base view) which I don't want , the subview should remain fixed at the bottom of the UIView.
Y position for the custom view is set to the height of the base view -
y = self.bounds.size.height - 40.0;
This works when the custom control is added for the first time, but does not work when the base view changes height.
I have set self.autoresizesSubviews = NO; for the base view.
If I remove the custom view from the base view and add it again after the height changes nothing is shown , just a blank base view.
Any ideas to fix the position of the custom view to the bottom of the base view.
Try to use
self.bounds = CGRectMake(0, 0, 320, 400);
It will make the bounds from top left corner to point on the right side of the screen that is 400 pixels below the initial point.
You can also try using method
-(void)layoutSubviews
I don't remember the correct syntax, but I'm pretty sure it was like that.
hope it helps
What about firing setNeedsDisplay method on that view.

Resources