programing

UI WebView 배경이 [Clear Color](색상 지우기)로 설정되어 있지만 투명하지는 않습니다.

stoneblock 2023. 4. 24. 21:10

UI WebView 배경이 [Clear Color](색상 지우기)로 설정되어 있지만 투명하지는 않습니다.

iOS SDK 최신 버전과 XCode 4.2를 사용하여 iOS 4 어플리케이션을 개발하고 있습니다.

XIB를 가지고 있습니다.UIWebViewAlpha = 1.0인 경우 배경Clear Color 및 Opaque설정되지 않습니다.이 XIB에서 다음 코드를 사용하여 이미지를 배경으로 설정합니다.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"AboutBackground.png"]];
        self.view.backgroundColor = background;
        [background release];
    }
    return self;
}

UIWebView는 스태틱 html을 나타내고 있습니다.

<html><head></head><body style=\"margin:0 auto;text-align:center;background-color: transparent; color:white\">...</body></html>

iOS 5 시뮬레이터에서는 배경이 투명하지만 iOS 4 디바이스에서는 회색입니다.

단서는?

또한 다음과 같이 설정합니다.

[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
     /*for ios please set this*/

     [webViewFirst setOpaque:NO];

     /*for html please set this*/
     <body style="background:none">

웹 뷰의 배경을 색을 클리어하도록 설정하는 것 외에 불투명도를 false로 설정합니다.

webView.opaque = NO;

webView.backgroundColor = [UIColor clearColor];

이 코드를 시험해 볼 수 있습니다(안전하지 않지만, ios5에서도 동작합니다).

- (void)makeBodyBackgroundTransparent {
        for (UIView *subview in [webView subviews]) {
            [subview setOpaque:NO];
            [subview setBackgroundColor:[UIColor clearColor]];
        }
        [webView setOpaque:NO];
        [webView setBackgroundColor:[UIColor clearColor]];
}

NSString *content=@"example clear background color UIWebview";
NSString *style=[NSString stringwithformat:@"<html><head><style>body {background-color:transparent;}</style></head><body>%@</body></html>",content];
[myWebview loadhtmlstring:style baseurl:nil];

최신 Swift 버전(필요에 따라):

lazy var webView: UIWebView = {
    let view = UIWebView()
    view.delegate = self
    view.backgroundColor = .clear
    view.isOpaque = false
    return view
}()

필요하지 않은 경우 대리점 라인 제거

목적의 경우

webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];

HTML 코드에 반드시 포함시켜 주세요.

<body style="background-color: transparent;">

또는

 <body style="background:none">

언급URL : https://stackoverflow.com/questions/8667150/uiwebview-background-is-set-to-clear-color-but-it-is-not-transparent