Fabio
Fabio

Reputation: 1973

Move all UIView from self.view.subviews

I need move all self.view.subviews to center of container and get support to iPhone 4 and i have this code but not work

iPhone 4

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

    if (IS_IPHONE_5) {
        
        NSLog(@"iphone 5");
    }else{
    
        NSLog(@"iphone 4");
        
        for (UIView *views in self.view.subviews) {
            
            CGRect frame = views.frame;
            frame.origin.x = 0; // new x coordinate
            frame.origin.y = 0; // new y coordinate
            views.frame = frame;
 
            
        }
    }

how do I get the reference of each object to center vertically in container?

In iPhone 5 works perfectly

iPhone 5

Upvotes: 0

Views: 2145

Answers (1)

Amar
Amar

Reputation: 13222

If I understand correctly, you want to adjust the starting x-coord of each subview such that it becomes vertically centred on the screen. For that you need to apply bit of math,

for (UIView *subview in self.view.subviews) {
    CGRect frame = subview.frame;
    frame.origin.x = (screenWidth-frame.size.width)/2; // new x coordinate
    subview.frame = frame;
}

Here, screenWidth can be taken programmatically. y-coord can adjusted as per the expected look and feel, keeping appropriate vertical gap between subviews.

Also, I feel you need not deal with this separately for iPhone5 and iPhone<5, you just need to program smartly. Usually I would deal with such screens by creating them using XIB. It provides lot of flexibility with features like autosizing, autolayout.

Hope that helps!

Upvotes: 3

Related Questions