ios - 从 self.view.subviews 移动所有 UIView

标签 ios iphone objective-c uiview

我需要将所有 self.view.subviews 移动到容器中心并获得对 iPhone 4 的支持,我有此代码但无法运行

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;
 
            
        }
    }

如何让每个对象的引用在容器中垂直居中?

在 iPhone 5 中完美运行

iPhone 5

最佳答案

如果我没理解错的话,您想调整每个 subview 的起始 x 坐标,使其在屏幕上垂直居中。为此,您需要应用一些数学知识,

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;
}

此处,screenWidth 可以通过编程方式获取。 y 坐标可以根据预期的外观和感觉进行调整,在 subview 之间保持适当的垂直间距。

另外,我觉得iPhone5和iPhone<5不用单独处理这个,你只需要巧妙地编程即可。通常我会通过使用 XIB 创建它们来处理此类屏幕。它通过自动调整大小、自动布局等功能提供了很大的灵 active 。

希望对您有所帮助!

关于ios - 从 self.view.subviews 移动所有 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19581838/

相关文章:

php - 403 禁止错误 Objective c to PHP

ios - UIImagePickerController StatusBar 为白色

iphone - 使用 AudioQueue 读取音频缓冲区数据

ios - UILabel如何收紧字母间距

javascript - 重定向到 iTunes 链接时如何避免 Safari 警报

ios - UIDatePicker 显示空选择器 View

ios - 如何将插图添加到 UILabel (iOS Swift)?

iphone - 有没有办法知道何时在 iOS 中删除应用程序?

objective-c - UITableView:显示 tableFooterView 时运行代码?

objective-c - 在 Objective-C 中模拟指向指针的指针