5

我通過繼承UIPopoverBackgroundView(使用本tutorial),並通過使用UIPopoverController提出它製作自酥料餅。不幸的是,只要我指定自定義popoverBackgroundViewClass本地灰色背景消失。使用自定義UIPopoverBackgroundView時,有什麼方法可以離開黯淡的背景嗎?我可以用來模擬原生行爲的任何其他解決方案?如何暗淡的背景添加到自定義UIPopoverBackgroundView

+0

請把你的代碼在這裏 – malex

+0

我更新了我的問題。我添加了我正在使用的教程的鏈接。 – sash

+0

不明白,爲什麼要投下來的問題?這不是關於如何繼承UIPopoverBackgroundView或者爲什麼它不工作。現在的問題是:是否有可能設置暗淡的背景開箱即用,如果你繼承UIPopoverBackgroundView。 – sash

回答

4

不知道爲什麼這下來了投票,這是一個很好的問題,因爲當你實現一個自定義UIPopoverBackgroundView,暗淡的背景下不被設置。在研究這個問題時,我確定了最好的方法就是自己設置!

剛剛創建酥料餅的觀點之前,我創建了一個「面具視圖」,將被添加到酥料餅的前視圖。此代碼包括實際上是一個不錯的漸漸以及:

self.customPopoverMaskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
    self.customPopoverMaskView.backgroundColor = [UIColor blackColor]; 
    self.customPopoverMaskView.alpha = 0.3f; 

    [UIView transitionWithView:self.view 
         duration:0.3 
         options:UIViewAnimationOptionTransitionCrossDissolve 
        animations:^ { 
         [self.view addSubview:self.customPopoverMaskView]; 
        } 
        completion:nil]; 

,並刪除視圖,插入該成處理酥料餅的視圖消失的方法(S):

[UIView transitionWithView:self.view 
        duration:0.3 
        options:UIViewAnimationOptionTransitionCrossDissolve 
       animations:^ { 
        [self.customPopoverMaskView removeFromSuperview]; 
       } 
       completion:nil]; 

很適合我。快樂的編碼!

亞倫

5

所有你需要的是接下來的代碼添加到initWithFrame:方法你實現UIPopoverBackgroundView的方法。

UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0 - self.frame.origin.x, 
                  0 - self.frame.origin.y, 
                  [UIScreen mainScreen].bounds.size.width, 
                  [UIScreen mainScreen].bounds.size.height)]; 
dimView.backgroundColor = [UIColor blackColor]; 
dimView.alpha = 0.15; 
dimView.userInteractionEnabled = NO; 
[self addSubview:dimView]; 

它的工作原理與默認的Apple實現相同。