每次我实例化一个 UIView 并使用 roundSpecificCorners:withRadius: 函数添加一个 UIBezierPath 作为遮罩时,该视图似乎在右侧和底部有较小的边距,导致它没有按预期覆盖整个视图。
@implementation LoginAccountViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initial setup of the view
enterEmailView.inputTextField.placeholder = @"Enter Email";
enterPasswordView.inputTextField.placeholder = @"Enter Password";
}
- (void)viewWillLayoutSubviews{
// Apply corner rounding on the main view
[mainViewContainer roundSpecificCorners:UIRectCornerTopLeft | UIRectCornerTopRight withRadius:10.0];
[super viewWillLayoutSubviews];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
__weak typeof(self) weakSelf = self;
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Apply corner rounding when changing orientation
UIWindowScene *windowScene = (UIWindowScene *)self.view.window.windowScene;
UIInterfaceOrientation orientation = windowScene.interfaceOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
[weakSelf.mainViewContainer roundSpecificCorners:UIRectCornerTopLeft | UIRectCornerTopRight withRadius:10.0];
} else {
[weakSelf.mainViewContainer roundSpecificCorners:UIRectCornerTopLeft | UIRectCornerTopRight withRadius:10.0];
}
} completion:nil];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
@end
@implementation UIView (CustomLoader)
// Other functions here...
- (void)roundSpecificCorners:(UIRectCorner)corners withRadius:(CGFloat)radius {
// Apply a layer mask with corner rounding
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)].CGPath;
self.layer.mask = maskLayer;
// Adjust the autoresizingMask to allow for size changes
self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleTopMargin;
}
为什么会发生这种行为?如何修复它,以便视图覆盖整个屏幕而没有任何额外的边距?