iOS 编程中心 UIView 组

标签 ios view uiview geometry centering

我想知道如何/如果我能做到这一点:将一组 UIView 置于父 View 的中心。最终结果将是一组“ View ”在其父 View 中明显居中。

最佳答案

好吧,这是一个两步过程;首先将 UIViews 添加到一个数组中,然后进行快速枚举以将它们作为一个“组”居中

//step 1: create the objects
self.label1= [[UILabel alloc]init];
self.label1.font=[UIFont systemFontOfSize:30.0];
self.label1.text=@"1";

self.label2= [[UILabel alloc]init];
self.label2.font=[UIFont systemFontOfSize:30.0];
self.label2.text=@"2";

self.label3= [[UILabel alloc]init];
self.label3.font=[UIFont systemFontOfSize:30.0];
self.label3.text=@"3";


//step2: create the array and add the objects to the array
self.arrayOfViews=[[NSMutableArray alloc]initWithCapacity:4];
[self.arrayOfViews addObject:self.label1];
[self.arrayOfViews addObject:self.label2];
[self.arrayOfViews addObject:self.label3];



//step3: use fast enumeration to be able to control them as "group"
for(UIView* currentViewObject in self.arrayOfViews)
{
    currentViewObject.alpha=0.3;
    currentViewObject.frame=CGRectMake(self.view.bounds.size.width/2-sizeOfObject/2,self.view.bounds.size.height/2-sizeOfObject/2,sizeOfObject,sizeOfObject);
    [self.view addSubview:currentViewObject];
}

第 1 步和 alpha 设置仅用于此处的测试目的。

希望这对您有所帮助。

关于iOS 编程中心 UIView 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15169244/

相关文章:

sql-server - 在架构内创建多个 View - SQL Server

grails - 在Grails中,如何从 Controller 返回到调用当前 View ?

ios - 关键帧动画关键时间

ios - 当我将一个 View 添加到另一个 View 时(这两个 View 都由 View Controller 控制),我真的应该使用 addChildViewController 吗?

ios - 是否有命令从 Firebase App Distribution 获取最新版本号,就像 TestFlight latest_testflight_build_number 一样?

ios - Swift 3 UIView 动画

ios - 调整 UIView 的约束。迅速

objective-c - 将 UIView 转换为 CALayer

ios - 推送通知自定义声音停止工作

ios - 从 UITabBarController 应用程序中的另一个 UIViewController 调用 UISplitViewController