自定义FullLoadingView高度异常(与父视图高度一致)技术问询
Fixing FullLoadingView Height Issue (Mismatched with Parent View)
Hey there, it looks like your custom FullLoadingView is having height alignment issues where it's not correctly fitting the parent view. The main culprit here is using hardcoded frame instead of AutoLayout constraints, plus some incomplete code and missing layout safety checks. Let's fix this step by step.
Complete Fixed Implementation of FullLoadingView.m
First, your original code cut off at [UICol — I'll assume you meant a semi-transparent background color. Here's the full corrected version using AutoLayout for proper adaptive layout:
#import "FullLoadingView.h" static const NSInteger kLoadingViewTag = 9999; // Unique tag to identify our loading views @implementation FullLoadingView + (instancetype)showOnView:(UIView *)view { // Prevent duplicate loading views on the same parent FullLoadingView *existingLoader = (FullLoadingView *)[view viewWithTag:kLoadingViewTag]; if (existingLoader) { return existingLoader; } FullLoadingView *loader = [[self alloc] init]; loader.tag = kLoadingViewTag; loader.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6]; // Semi-transparent background loader.translatesAutoresizingMaskIntoConstraints = NO; // Disable auto-translated constraints for manual AutoLayout // Add activity indicator (you can customize this with a label or other elements) UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityIndicator.translatesAutoresizingMaskIntoConstraints = NO; [activityIndicator startAnimating]; [loader addSubview:activityIndicator]; // Center the indicator in the loading view [NSLayoutConstraint activateConstraints:@[ activityIndicator.centerXAnchor.constraintEqualToAnchor:loader.centerXAnchor, activityIndicator.centerYAnchor.constraintEqualToAnchor:loader.centerYAnchor ]]; [view addSubview:loader]; // Pin loading view to all edges of parent view [NSLayoutConstraint activateConstraints:@[ loader.topAnchor.constraintEqualToAnchor:view.topAnchor, loader.leadingAnchor.constraintEqualToAnchor:view.leadingAnchor, loader.trailingAnchor.constraintEqualToAnchor:view.trailingAnchor, loader.bottomAnchor.constraintEqualToAnchor:view.bottomAnchor ]]; return loader; } + (void)hideAll:(UIView *)view { FullLoadingView *loader = (FullLoadingView *)[view viewWithTag:kLoadingViewTag]; [loader hide]; } - (void)hide { [UIView animateWithDuration:0.3 animations:^{ self.alpha = 0.0; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } @end
Why Your Original Code Had Issues
- Hardcoded Frame Dependence: Setting
frame = view.boundsonly captures the parent view's size at the moment you call the method. If the parent view hasn't finished laying out (e.g., called inviewDidLoadbefore the view is sized for the screen) or if the parent's layout changes later (screen rotation, dynamic content size changes), your loading view won't update its size automatically. - No AutoLayout Constraints: In modern iOS development, AutoLayout is the standard for adaptive layouts. Using
framedirectly can lead to layout conflicts or incorrect sizing, especially when working with safe areas or dynamic view hierarchies. - Duplicate View Risk: Your original code didn't check for existing loading views, so multiple calls to
showOnView:would stack multiple masks on top of each other.
Usage Tips
- If you want the loading view to respect the parent's safe area (e.g., avoid the notch or home indicator), replace the edge constraints with
safeAreaLayoutGuideanchors:loader.topAnchor.constraintEqualToAnchor:view.safeAreaLayoutGuide.topAnchor, loader.bottomAnchor.constraintEqualToAnchor:view.safeAreaLayoutGuide.bottomAnchor - Call
showOnView:after the parent view has finished laying out (e.g., inviewDidAppear:orviewDidLayoutSubviews) for the most accurate initial layout, though the AutoLayout constraints will handle updates automatically regardless.
This implementation will ensure your FullLoadingView always perfectly covers its parent view, no matter how the parent's size changes.
内容的提问来源于stack exchange,提问作者Luciano Nascimento
相关产品推荐
相关产品推荐

