How to get frame of subview after apply transform on mainView?


I have created mainView objcet of UIView and added one subview on it. I applied transform on mainView for reducing frame size. But frame of subview of mainView was not reduced. How to reduce the size of this subview.

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGFloat widthM=1200.0; CGFloat heightM=1800.0; UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, widthM, heightM)]; mainView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"te.png"]]; [self.view addSubview:mainView]; CGFloat yourDesiredWidth = 250.0; CGFloat yourDesiredHeight = yourDesiredWidth *heightM/widthM; CGAffineTransform scalingTransform; scalingTransform = CGAffineTransformMakeScale(yourDesiredWidth/mainView.frame.size.width, yourDesiredHeight/mainView.frame.size.height); mainView.transform = scalingTransform; mainView.center = self.view.center; NSLog(@"mainView:%@",mainView); UIView *subMainView= [[UIView alloc] initWithFrame:CGRectMake(100, 100, 1000, 1200)]; subMainView.backgroundColor = [UIColor redColor]; [mainView addSubview:subMainView]; NSLog(@"subMainView:%@",subMainView); }

NSlog of these views:

mainView:<UIView: 0x8878490; frame = (35 62.5; 250 375); transform = [0.208333, 0, 0, 0.208333, 0, 0]; layer = <CALayer: 0x8879140>> subMainView:<UIView: 0x887b8c0; frame = (100 100; 1000 1200); layer = <CALayer: 0x887c160>>

Here the width of mainView is 250, the width of subview is 1000. but when i get the output in simulator, subview is occupied correctly, but it's not cross the mainView. How it is possible? How to get frame of subview with respect mainView frame after transformation?


What you're seeing is expected behavior. The frame of an UIView is relative to its parent, so it doesn't change when you apply a transformation to its superview. While the view will appear 'distorted' too, the frame won't reflect the changes since it's still at exact the same position relative to its parent.<br /> However, I assume you would like to get the frame of the view relative to the topmost UIView. In that case UIKit offers these functions:

<ul><li>– [UIView convertPoint:toView:]</li> <li>– [UIView convertPoint:fromView:]</li> <li>– [UIView convertRect:toView:]</li> <li>– [UIView convertRect:fromView:]</li> </ul>

I applied these to your example:

CGRect frame = [[self view] convertRect:[subMainView frame] fromView:mainView]; NSLog(@"subMainView:%@", NSStringFromCGRect(frame));

And this is the output:

subMainView:{{55.8333, 83.3333}, {208.333, 250}}


In addition to s1m0n answer, the beautiful thing about applying a transform matrix to your view, is that you can keep reasoning in terms of its original coordinate system (in your case, you can handle subMainView using the non-transformed coordinate system, which is why, even though subMainView's frame is bigger than mainView's transformed frame, it still doesn't cross the parent view, as it gets automatically transformed). This means that when you have a transformed parent view (for example rotated and scaled) and you want to add a subview in a particular point relative to this parent view, you don't have to first keep track of the previous transformations in order to do so.

If you really are interested in knowing the subview's frame in terms of the <em>transformed</em> coordinate system, it will be enough to apply the same transformation to the subview's rectangle with:

CGRect transformedFrame = CGRectApplyAffineTransform(subMainView.frame, mainView.transform);

If you then NSLog this CGRect, you will obtain:

Transformed frame: {{20.8333, 20.8333}, {208.333, 250}}

Which, I believe, are the values that you were looking for. I hope this answers your question!


  • How to show an UIView on top of an UITableView?
  • Rotate UIImage in xamarin iOS
  • Swift Animate duration not working in CGAffineTransform
  • Draw a simple vertical bar in iphone
  • Pinch Gesture not working with imageVIew
  • NSFileHandle from NSURL failure
  • Call same +load implementation on all subclasses
  • iPhone 5: AVCaptureExposureModeAutoFocus not supported in iOS 7
  • Getting NSDate from NSString
  • How to set view of programatically created view controller to SKView?
  • How can i get the locale from currency names list
  • TouchesEnded not working in iOS5, working fine in iOS4
  • I am sending dictionary in json and its have two array and multiple value but i am getting response
  • iPhone App how to post Youtube video link using facebook graph Api
  • Store and fetch documents in/from app documents folder
  • running out of memory from for loop
  • Detecting Ctrl + Return or Ctrl + Enter presses
  • Converting NSString to Float - Adds in Decimal Places?
  • Drag and drop without removing UIButton
  • Image taken in Portrait is distorted when viewed in Landscape
  • applicationWillTerminate not called
  • Can't add target for UIButton - unrecognised selector sent to instance, despite method been in
  • Cocos2D set sprite position in relation to another sprite
  • XMPP push notifications causing problems (delay + duplications) in messages
  • Add custom information to HockeyApp crash report
  • How to use CKFetchNotificationChangesOperation?
  • this class is not key value coding-compliant for the key PerformRotate
  • Converting Long 64-bit Decimal to Binary
  • a simple person class in objective c
  • Why won't my UITableViewCell deselect and update its text?
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • blade.php method outputting it's result to the form
  • D3 get axis values on zoom event
  • Saving Changes After In-App Purchase Has Been Purchased
  • QLPreviewController hide print button in ios6
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • How to show dropdown in excel using jrxml (jasper api)?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • python draw pie shapes with colour filled