【问题标题】:iOS popover how to remove the shadeiOS弹出框如何去除阴影
【发布时间】:2026-01-26 09:20:02
【问题描述】:

每当我们显示弹出框时,相邻区域都会显示不同的灰色,并且活动标签栏图标颜色会从蓝色变为灰色,直到弹出框出现。

当弹出框被关闭时,灰色阴影被移除

我想在弹出框可见时移除颜色

我用谷歌搜索,但无论如何我都找不到这种默认行为。

感谢任何帮助我解决问题的帮助

谢谢

【问题讨论】:

    标签: ios objective-c uitabbarcontroller uipopovercontroller


    【解决方案1】:

    为此,您可以创建自己的自定义弹出框背景UIPopoverBackgroundView。 您可以找到以下用于创建自定义 CustomPopoverBgView 的代码。

    CustomPopoverBgView.h

    #import <UIKit/UIKit.h>
        @interface CustomPopoverBgView : UIPopoverBackgroundView
    {
        UIImageView *_borderImageView;
        UIImageView *_arrowView;
        CGFloat _arrowOffset;
        UIPopoverArrowDirection _arrowDirection;
    }
    @end
    

    CustomPopoverBgView.m

    #import "CustomPopoverBgView.h"
    #define CONTENT_INSET 10.0
    #define CAP_INSET 25.0
    #define ARROW_BASE 25.0
    #define ARROW_HEIGHT 25.0
    -(instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if(self){
            self.layer.shadowColor = [[UIColor clearColor] CGColor];
            _borderImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"popover-bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(CAP_INSET,CAP_INSET,CAP_INSET,CAP_INSET)]];
    
            _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
    
            [self addSubview:_borderImageView];
            [self addSubview:_arrowView];
        }
        return self;
    }
    +(UIEdgeInsets)contentViewInsets{
        return UIEdgeInsetsMake(CONTENT_INSET, CONTENT_INSET, CONTENT_INSET, CONTENT_INSET);
    }
    
    +(CGFloat)arrowHeight{
        return ARROW_HEIGHT;
    }
    
    +(CGFloat)arrowBase{
        return ARROW_BASE;
    }
    - (CGFloat) arrowOffset {
        return _arrowOffset;
    }
    
    - (void) setArrowOffset:(CGFloat)arrowOffset {
        _arrowOffset = arrowOffset;
    }
    - (void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection {
        _arrowDirection = arrowDirection;
    }
    - (UIPopoverArrowDirection)arrowDirection {
        return _arrowDirection;
    }
    @end
    

    UIPopoverController中调用CustomPopoverBgView

     UIButton *btn = (UIButton *)sender;
     ViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"dddddd"];
     controller.view.backgroundColor = [UIColor redColor];
     UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:controller] ;
    
     popoverController.popoverBackgroundViewClass = [CustomPopoverBgView class];
    [popoverController presentPopoverFromRect:btn.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:true];
    

    希望这会对你有所帮助。

    【讨论】:

    • 您可以通过提供演示自定义背景视图用法的代码来改进您的答案。
    • 如何使用它来代替 popovercontroller ?请解释一下谢谢
    • 非常感谢它有效。我错过了向您分配赏金对此感到抱歉。弹出窗口出现时标签栏颜色变为灰色.. 是否也可以为此分配 clearcolor 跨度>
    【解决方案2】:

    创建您自己的自定义弹出视图并将其作为子视图添加到您的主视图之上,而不是 Apple 提供的默认视图。

    【讨论】:

    • 它看起来不像弹出框,我的意思是在其他视图之上。我的意思是其他视图必须在自定义弹出框视图下方
    • 是的,这就是为什么你需要一个阴影让它看起来像一个弹出视图