在Objective-C中,要删除顶部和底部空间,可以通过以下步骤实现:
例如,如果你的视图是一个UILabel,并且你想要将其顶部与父视图的顶部对齐,可以使用以下代码:
NSLayoutConstraint *topConstraint = NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0;
self.view addConstraint:topConstraint;
例如,如果你的视图是一个UIButton,并且你想要将其底部与父视图的底部对齐,可以使用以下代码:
NSLayoutConstraint *bottomConstraint = NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0;
self.view addConstraint:bottomConstraint;
topConstraint.active = YES;
bottomConstraint.active = YES;
通过以上步骤,你可以在Objective-C中删除顶部和底部空间,使视图紧贴着顶部和底部边界。
领取专属 10元无门槛券
手把手带您无忧上云