Last active
March 9, 2016 11:01
-
-
Save erwinmaza/5000828 to your computer and use it in GitHub Desktop.
Fix for centering thumb image when rotating UISlider 90 degrees to a vertical orientation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
UISlider controls can be rotated to a vertical orientation with this setting: | |
layer.transform.rotation.z = 1.570795 (type = String) | |
in IB under User Defined Runtime Attibutes for the UISlider. | |
However, this makes it visually obvious that iOS does not draw the thumb image | |
over the center of the slider track, it is actually offset 1 point up (when horizontal). | |
The following code fixes that. | |
*/ | |
- (void)viewDidAppear:(BOOL)animated { | |
[super viewDidAppear:animated]; | |
[myVerticalSlider setThumbImage:[self offsetThumbImage:[myVerticalSlider thumbImageForState:UIControlStateNormal]] forState:UIControlStateNormal]; | |
[myVerticalSlider setThumbImage:[self offsetThumbImage:[myVerticalSlider thumbImageForState:UIControlStateHighlighted]] forState:UIControlStateHighlighted]; | |
} | |
- (UIImage*)offsetThumbImage:(UIImage*)image { | |
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height); | |
UIGraphicsBeginImageContextWithOptions(imageRect.size, FALSE, 0.0); | |
[image drawInRect:CGRectMake(0, 1, image.size.width, image.size.height)]; | |
image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment