![](/img/trans.png)
[英]Adding UIActivityIndicatorView to the top view of a UIButton
[英]UIButton on top of view with userInteractionEnabled=NO
我在keyWindow
頂部創建一個透明層
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
_topLayer = [[UIView alloc] initWithFrame:window.frame];
_topLayer.backgroundColor = [UIColor clearColor];
_topLayer.userInteractionEnabled = NO;
[window addSubview:_topLayer];
我要添加要顯示在所有內容之上的子視圖。 我用了
userInteractionEnabled = NO
因此該層不會阻塞對下層員工的水龍頭。
我試圖在該層的頂部添加一個UIButton,但是由於上面的userInteractionEnabled = NO
,它沒有響應點擊
如何在保持整個背景透明的同時讓按鈕具有響應性?
先感謝您!
您可以執行以下操作:使用userInteractionEnabled=YES
subclass
化UIView
,然后使用以下代碼段:
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
id hitView = [super hitTest:point withEvent:event];
if (hitView == self) return nil; // <--- pass-through if touch on UIView
else return hitView; // touch on children
}
@anhtu的答案是我要實現的目標的真正復雜之處。 正確的方法是將按鈕設置為透明視圖的同級,而不是將其添加為子視圖。
在此處查看對此的擴展: https : //stackoverflow.com/a/35592676/913347
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.