1

我在渲染具有自定义 uiappearance 集的滑块的视图控制器的图像时遇到问题。图像渲染良好,但是图像中的滑块只有默认外观。如果我转到该视图控制器 (vc2),则 uiappearance 将设置为自定义外观。任何想法如何使用自定义滑块外观呈现图像?

我在第一个视图控制器上有一个 UIImageView。在第二个视图控制器上,我有一个滑块。

视图控制器.m

ViewController *vc2 = [[ViewController alloc]init];
vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"];
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,NO,0.0);
[vc2.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[_image setImage:viewImage];

AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary      *)launchOptions
    {
    [self customizeAppearance];
    // Override point for customization after application launch.
    return YES;
    }
    - (void) customizeAppearance; {
    UIImage *minImage = [[UIImage imageNamed:@"slider_minimum.png"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 0)];
    UIImage *maxImage = [[UIImage imageNamed:@"slider_maximum.png"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 13)];
    UIImage *thumbImage = [UIImage imageNamed:@"slider_thumb.png"];

    [[UISlider appearance] setMaximumTrackImage:maxImage
                                       forState:UIControlStateNormal];
    [[UISlider appearance] setMinimumTrackImage:minImage
                                       forState:UIControlStateNormal];
    [[UISlider appearance] setThumbImage:thumbImage
                                forState:UIControlStateNormal];
    [[UISlider appearance] setThumbImage:thumbImage
                                forState:UIControlStateHighlighted];

    }
4

1 回答 1

0

感谢@PartiallyFinite 的指导,我发现我需要在 ViewController 内部执行以下操作:

UIImage *minImage = [UIImage imageNamed:@"slider_minimum.png"];
UIImage *maxImage = [UIImage imageNamed:@"slider_maximum.png"];
UIImage *tumbImage= [UIImage imageNamed:@"slider_thumb.png"];

minImage = [minImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 3, 0, 0)];
maxImage = [maxImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 13)];

[_slider setMinimumTrackImage:minImage forState:UIControlStateNormal];
[_slider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[_slider setThumbImage:tumbImage forState:UIControlStateNormal];
于 2013-07-31T06:10:03.397 回答