【问题标题】:How to add SubView in iOS programmatically?如何以编程方式在 iOS 中添加 SubView?
【发布时间】:2015-12-17 12:35:25
【问题描述】:

最初,我创建了名为 MainView 的视图。在该视图中我放置了名为 UnitsView 的视图。我的问题是:如果我想在 MainView 上添加 UnitsView 意味着,我是否必须使用 self.view 或 MainView 。

UIView *MainView = [[UIViewalloc]initwithframe: CGRectMake    (0,0,self.view.frame.size.width, self.view.frame.size.height) ];
TopView.backgroundColor = [UIColor grayColor];
[self.view addSubview:TopView];

UIView *UnitsView = [[UIView alloc]init];
[self.view (or)MainView addsubView:UnitsView];

【问题讨论】:

标签: ios subview


【解决方案1】:
UIView *UnitsView = [[UIView alloc]initwithframe: CGRectMake(0,0,width,height)];
[MainView addsubView:UnitsView];
[self.view addSubView:MainView];

【讨论】:

  • k 谢谢...但很抱歉,我无法为 bcoz 投票,我需要 15 个声望。@vaibby 一件事:现在我想为 UnitsView 添加自动布局约束。
  • 可以的话现在试试
【解决方案2】:

在视图上方添加视图:-

UIView *UnitsView = [[UIView alloc]initwithframe:CGRectMake(0,0,100,100)];
UnitsView.backgroundColor = [UIColor grayColor];
[self.view addSubView:UnitsView];


UIView *SubUnit = [[UIView alloc]initwithframe:CGRectMake(0,0,50,50)];
SubUnit.backgroundColor = [UIColor greenColor];
[self.UnitsView addSubView:SubUnit];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多