ios – 如何以编程方式将编程方式添加到以编程方式创建的UIView
发布时间:2020-12-15 01:54:56 所属栏目:百科 来源:网络整理
导读:我使用viewDidLoad中的以下代码创建了一个UIView(其中’secondview’显然是UIView的名称): secondview = [[UIView alloc] initWithFrame:self.view.frame]; [secondview setBackgroundColor: [UIColor yellowColor]]; secondview.translatesAutoresizingMas
我使用viewDidLoad中的以下代码创建了一个UIView(其中’secondview’显然是UIView的名称):
secondview = [[UIView alloc] initWithFrame:self.view.frame]; [secondview setBackgroundColor: [UIColor yellowColor]]; secondview.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:secondview]; 然后在viewDidAppear中,我添加了这个视图的约束: NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:secondview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-20.0f]; [self.view addConstraint:constraint]; 但是,约束不适用于视图(至少不是我可以看到的)。相反,该视图似乎从屏幕上消失。然而,如果约束代码被注释掉,则视图再次加载适当的框架(显然没有使用约束)。当对Button或ImageView应用相同的约束时,约束将被完美地应用。这导致我认为问题是因为创建视图时的’initWithFrame’,因为按钮和ImageView实际上都不需要指定它的大小。 你怎么看?我该怎么做不同 解决方法
对于遇到这个的任何人,我需要添加多个约束。这样做了。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |