Ios 更改自定义UIView及其父级VC底部布局的约束
我有一个自定义UIView,我想在我的项目的一半以上的VCs上反复使用它。此外,在所有这些VCs上,它应该位于底部 出于某些原因,我想在操作时使用bottomLayoutGuide更改其底部约束。如果我不关心重用这个结构,那么很容易做到。然后,我可以使用我的视图的outlet来更改此约束。然而,这意味着我必须在其他VCs中复制代码Ios 更改自定义UIView及其父级VC底部布局的约束,ios,swift,xcode,uiview,Ios,Swift,Xcode,Uiview,我有一个自定义UIView,我想在我的项目的一半以上的VCs上反复使用它。此外,在所有这些VCs上,它应该位于底部 出于某些原因,我想在操作时使用bottomLayoutGuide更改其底部约束。如果我不关心重用这个结构,那么很容易做到。然后,我可以使用我的视图的outlet来更改此约束。然而,这意味着我必须在其他VCs中复制代码 如何避免这种重复并重用我的自定义视图,当前VC bottomLayoutGuide的约束可以在操作时更改?创建一个类,它是nsobject的子类,并像这样编写函数。
如何避免这种重复并重用我的自定义视图,当前VC bottomLayoutGuide的约束可以在操作时更改?创建一个类,它是nsobject的子类,并像这样编写函数。 在该子类中导入之前的框架
+ (UIView*)makeCustomViewWithBottomConstraint:(NSInteger)bottomConstant andHeight:(NSInteger)viewHeight inViewController:(UIViewController*)vc{
UIView *customView = [[UIView alloc]init];
[vc.view addSubview:customView];
customView.backgroundColor = [UIColor purpleColor];
customView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *metrics = @{@"viewHeight":@(viewHeight),
@"bottomConstant":@(bottomConstant)};
NSDictionary *views = NSDictionaryOfVariableBindings(customView);
NSArray *hConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|"
options:0
metrics:metrics
views:views];
NSArray *vConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(viewHeight)]-bottomConstant-|"
options:0
metrics:metrics
views:views];
[vc.view addConstraints:hConstraint];
[vc.view addConstraints:vConstraint];
return customView;
}
并在所需的视图控制器中访问该类方法,如下所示
[CustomViewClass makeCustomViewWithBottomConstraint:0 andHeight:40 inViewController:self];
我已经在GitHub中添加了示例项目。对不起,我仍然喜欢这种语言。
添加该代码,以便我可以帮助您优化该代码。