Created
December 22, 2014 11:56
-
-
Save alexrepty/d7a9a9b7c10b8f8d75e8 to your computer and use it in GitHub Desktop.
One way to use a custom reorder control in iOS 8 apps.
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
- (void)addSubview:(UIView *)view { | |
// For the system-supplied reorder control, we want to eliminate the default | |
// image and instead use our own. In order to achieve this, we need to use | |
// the somewhat hacky way of confirming that this is indeed a system-created | |
// UITableViewCellReorderControl by checking the class name. Icky? Icky. | |
NSArray *relevantClassnameParts = @[@"ReorderControl"]; | |
NSString *viewClassname = NSStringFromClass([view class]); | |
for (NSString *classname in relevantClassnameParts) { | |
if (![viewClassname containsString:classname]) { | |
// This is not the droid I'm looking for. | |
continue; | |
} | |
// Make sure to remove all existing subviews from the reorder container | |
// view. | |
for (UIView *currentSubview in view.subviews) { | |
[currentSubview removeFromSuperview]; | |
} | |
// Create a UIImageView with the image we would actually like to use as | |
// a reorder control, add it to the container view and set up layout | |
// constraints to keep it centered within its container. | |
UIImageView *controlImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"reorder_control"]]; | |
controlImageView.translatesAutoresizingMaskIntoConstraints = NO; | |
[view addSubview:controlImageView]; | |
[view addConstraint:[NSLayoutConstraint constraintWithItem:controlImageView | |
attribute:NSLayoutAttributeCenterX | |
relatedBy:NSLayoutRelationEqual | |
toItem:view | |
attribute:NSLayoutAttributeCenterX | |
multiplier:1.0 | |
constant:0.0]]; | |
[view addConstraint:[NSLayoutConstraint constraintWithItem:controlImageView | |
attribute:NSLayoutAttributeCenterY | |
relatedBy:NSLayoutRelationEqual | |
toItem:view | |
attribute:NSLayoutAttributeCenterY | |
multiplier:1.0 | |
constant:0.0]]; | |
} | |
[super addSubview:view]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very prone to break in future iOS versions, but it's the best way I could find. Iterating through the view hierarchy is even more dangerous, IMO.