Search code examples
objective-csfsafariviewcontroller

SFSafariViewController background button color using global app colors


My app uses this line below to change all buttons to use this certain color. It also turns the buttons in my SFSafariViewController the same color which looks awful. However I cant find a good way to fix this. The only way I did it was the code below but there is an issue after it.

[[UIButton appearance] setBackgroundColor:[Helper getColor:self.application.color]];

Works because of the setBackgroundColor, but once I am done with the browser and go back to the app all my buttons are clear.

[[UIButton appearance] setBackgroundColor:[UIColor clearColor]];
        SFSafariViewController *safariViewController = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:self.message.filePath]];
        [self presentViewController:safariViewController animated:true completion:nil];

This didnt work either.

[[UIButton appearanceWhenContainedIn:[SFSafariViewController class], nil] setBackgroundColor:[UIColor clearColor]];

There has to be an easier way to do this. You can see the color of the app is orange and does not look good below in the Safari Browser.

enter image description here


Solution

  • Looks like the only way to do it is to create a custom view controller and keep track of the previous color.

    @implementation BrowserViewController 
    
    @synthesize appColor;
    
    -(instancetype)initWithURL:(NSURL *)URL {
        appColor = [[UIButton appearance] backgroundColor];
        [[UIButton appearance] setBackgroundColor:[UIColor clearColor]];
        return [super initWithURL: URL];
    
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [[UIButton appearance] setBackgroundColor:appColor];
        [super viewWillDisappear:animated];
    }
    
    - (void)viewDidUnload {
    
        self.appColor = nil;
    
        [super viewDidUnload];
    }
    
    
    
    @end