Nice programing

iOS에서 UIWebView의 배경을 변경할 수 없습니다.

nicepro 2020. 12. 9. 21:45
반응형

iOS에서 UIWebView의 배경을 변경할 수 없습니다.


배경색을 변경하는 방법이 UIWebView있습니까?

IB 효과 UIWebView동작에 설정된 색상이 없습니다. 실제 콘텐츠가로드되기 전에 흰색으로 표시됩니다 (로드되는 순간과 콘텐츠가 렌더링되는 시점 사이에 흰색 깜박임이 발생 함).

프로그래밍 방식으로 배경색을 설정해도 아무 작업도 수행되지 않습니다.

다음은 코드입니다.

@interface Web_ViewViewController : UIViewController {

    UIWebView *web;
}

@property (nonatomic, retain) IBOutlet UIWebView *web;
@end

....
-(void)viewDidLoad {
    super viewDidLoad;

    web.backgroundColor = [UIColor blueColor];
    NSURL *clUrl = [NSURL URLWithString:@"http://www.apple.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];

    [web loadRequest:req];
}

웹보기의 불투명 속성을 NO로 설정 한 다음 아래보기의 배경색을 설정할 수 있습니다.

[webView setOpaque:NO];

이것을 시도하십시오. 로드가 완료되면 UIWebView가 페이드 인되고 플래시가 표시되지 않습니다.

@interface Web_ViewViewController : UIViewController <UIWebViewDelegate> {

UIWebView *web;
BOOL firstLoad;
}

@property (nonatomic, retain) IBOutlet UIWebView *web;
@end

...

(void)viewDidLoad {
    [super viewDidLoad];
    firstLoad = YES;
    web.delegate = self;
    web.alpha = 0.0;
    NSURL *clUrl = [NSURL URLWithString:@"http://www.apple.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];
    [web loadRequest:req];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (firstLoad) {
    firstLoad = NO;
    [UIView beginAnimations:@"web" context:nil];
    web.alpha = 1.0;
    [UIView commitAnimations];
    }
}

webViewDidFinishLoad의 애니메이션 블록은로드 된 뷰를 페이드 인하거나 팝업을 표시하려는 경우 UIView 호출을 제거합니다.


여기에 나온 답변 중 어느 것도 나를 위해 일하지 않았으며 모두 여전히 일종의 플래시와 관련이 있습니다. 나는 여기에서 작동하는 대답을 찾았습니다 : http://www.iphonedevsdk.com/forum/iphone-sdk-development/4918-uiwebview-render-speeds-white-background.html (나는 지연을 피하기 위해 .25로 조정해야했습니다. 깜박임). InterfaceBuilder에서 UIWebView의 "hidden"플래그를 확인하는 것을 잊지 마십시오.

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    [self performSelector:@selector(showAboutWebView) withObject:nil afterDelay:.25];          
}

- (void)showAboutWebView 
{
    self.infoWebView.hidden = NO;
}

예, backgroundColor 속성을 설정해도 UIWebView에 영향을 미치지 않는 것 같습니다.

만족스러운 해결책이 없으며 고려할 수있는 해결 방법 만 있습니다.

  • 먼저 초기 빈 HTML 페이지를 설정하여 배경색을 변경합니다.
  • 로드되면 기본 URL (예 : http://www.apple.com ) 을로드합니다.

If you don't wait for the initial HTML page to load, you might not see the initial background while the main URL is loading. So you'll need to use the UIWebViewDelegate's webViewDidFinishLoad: method. You can implement that method in your Web_ViewViewController class, and make your Web_ViewViewController conform to the UIWebViewDelegate protocol. Initially, there is still a momentary flicker of white background until the empth HTML page loads. Not sure how to get rid of that. Below is a sample:

- (void)viewDidLoad
{
    [web loadHTMLString: @"<html><body bgcolor=\"#0000FF\"></body></html>" baseURL: nil];
    web.delegate = self;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    static BOOL loadedMainURLAlready = NO;
    if (!loadedMainURLAlready)
    {
        NSURL *clUrl = [NSURL URLWithString:@"http://apple.com"];
        NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];
        [webView loadRequest:req];
        loadedMainURLAlready = YES;
    }
}

I got a solution. Add the web view after it is loaded.

-(void)webViewDidFinishLoad: (UIWebView *)pageView {

    self.view =pageView;
    [pageView release];
}

Try this out. I have used a background image "address.png" from my application resources.

NSString *path=[[NSBundle mainBundle] pathForResource:@"address" ofType:@"png"];

NSURL *a=[[NSURL alloc] initFileURLWithPath:[path stringByDeletingLastPathComponent] isDirectory:YES];


NSLog(@"%@",[a description]);

NSDictionary *d=[parsedarray objectAtIndex:0];
// generate dynamic html as you want.
NSMutableString *str=[[NSMutableString alloc] init];
[str appendString:@"<html><body background=\"address.png\"><table><tr>"];
[str appendFormat:@"<td valign=\"top\" align=\"left\"><font size=\"2\"><b>Market Address</b><br>%@,<br>%@,<br>%@</font></td>",
             ([d valueForKey:@"address1"])?[d valueForKey:@"address1"]:@"",
             ([d valueForKey:@"address2"])?[d valueForKey:@"address2"]:@"",
             ([d valueForKey:@"address3"])?[d valueForKey:@"address3"]:@""
             ];

[str appendFormat:@"<td valign=\"top\" align=\"left\"><font size=\"2\"><b>Contact Number</b><br>%@<br><b><a href=\"%@\">Email Us</a><br><a href=\"%@\">Visit Website</a></b></font></td>",
[d valueForKey:@"phone"],[d valueForKey:@"email"],[d valueForKey:@"website"]];
[str appendString:@"</tr></table></body></html>"];


// set base url to your bundle path, so that it can get image named address.png 
[wvAddress loadHTMLString:str baseURL:a];

[str release]; str=nil;
[a release];

In Xcode 4.6.3

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.viewWeb.hidden = YES;
    self.viewWeb.delegate = self;
// this allows the UIWebView box to your background color seamlessly.
    self.viewWeb.opaque = NO;

}

This might help

(Xcode 5 iOS 7) Universal App example for iOS 7 and Xcode 5. It is an open source project / example located here: Link to SimpleWebView (Project Zip and Source Code Example)

webview.backgroundColor = [UIColor clearColor];

참고URL : https://stackoverflow.com/questions/1547102/cant-change-background-for-uiwebview-in-ios

반응형