×

iOS CGRectGetMidX & CGRectGetMidY


CGRectGetMidX & CGRectGetMidY ?

CGRectGetMidX is used to get the middle point of a screen in x-Direction .

CGRectGetMidY is used to get the middle point of a screen in y-Direction .

Basics of X,Y Position in iOS UI Design :-


Example :-

In this example we will create a Square View of 200 X 200 px. Final Output will be look like this....


//  ViewController.m
//  Created by Manish Methani on 31/03/17.
//  Copyright © 2017 Codzify. All rights reserved.
//


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *superView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame)-100, CGRectGetMidY(self.view.frame)-100, 200, 200)];
    [self.view addSubview:superView];
    superView.backgroundColor = [UIColor orangeColor];
    
}

In this example, we created a superView .

CGRectMake(CGRectGetMidX(self.view.frame)-100, CGRectGetMidY(self.view.frame)-100, 200, 200)

Frame of SuperView :-

1) x-Coordinate = CGRectGetMidX(self.view.frame)-100

By CGRectGetMidX we get middle point of self.view as specified in paranthesis on x-Axis. Then we want square view of 200px .Right? So Imagine middle point of screen in x-direction and 100px of square on left side of that middle point and 100px of square view on right side of middle point. That's why (- 100px); It's all about imagination. Practice it.

2) y-Coordinate = CGRectGetMidY(self.view.frame)-100

By CGRectGetMidY we get middle point of self.view as specified in paranthesis on y-Axis. Then we want square view of 200px .Right? So Imagine middle point of screen on y-Axis and 100px of square on top side of that middle point and 100px of square view on bottom side of middle point. That's why (- 100px); It's all about imagination. Practice it.