在公司上个版本的开发中遇到了一个UI布局的小问题:

原理:获取到keywindow,然后在keywindow上面添加一个bgView,在bgView上面加上一个UIButton(之所以这样包装一层是为了美观一点,想将shareBtn弄成圆型)

    - (void)createShareBtn
{
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(0, 0, 40, 40);
    [shareBtn setImage:[UIImage imageNamed:@"wright"] forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(shareBtnAction) forControlEvents:UIControlEventTouchUpInside];
   
    bgView = [[UIView alloc] initWithFrame:CGRectMake(kScreen_width-50, kScreen_height/2-64, 40, 40)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.alpha = .9;
    bgView.layer.cornerRadius = 20;
    bgView.layer.masksToBounds = YES;
    [bgView addSubview:shareBtn];
    
    // 获取keyWindow
    UIWindow *keywindow = [UIApplication sharedApplication].keyWindow;
    [keywindow addSubview:bgView];
}
- (void)shareBtnAction
{
//创建分享视图
    [self shareView];
}

有一点需要注意:即如果我们这样设置了,那么需要进行下一步操作(在视图pop的时候,移除shareBtn),否则shareBtn会一直显示在keywindow上面

    - (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
// 这里bgView是全局的
    [bgView removeFromSuperview];
}

当然还有一点

为此我做了如下设置:

1、值得说一下的是,整个二级页面的层级结构是

UIViewController-->webView

2、让我们来看看webView里有一些什么我们可以用到的:

@interface UIWebView : UIView <NSCoding, UIScrollViewDelegate> 
@property (nullable, nonatomic, assign) id <UIWebViewDelegate> delegate;
@property (nonatomic, readonly, strong) UIScrollView *scrollView NS_AVAILABLE_IOS(5_0);

对于UIScrollViewDelegate大家应该不陌生了

    #pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    bgView.alpha = .45;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    bgView.alpha = .9;
}

当然一定不要忘了签约:webView.scrollView.delegate = self;

对于- (void)createShareBtn方法的调用随大家需求所定

1、如果需要长期显示,可以在- (void)viewDidLoad方法里调用(此处还有一个小技巧:createShareBtn在createWebView之后

05-11 18:17