Skip to content

Instantly share code, notes, and snippets.

@alexrepty
Created December 22, 2014 11:56
Show Gist options
  • Save alexrepty/d7a9a9b7c10b8f8d75e8 to your computer and use it in GitHub Desktop.
Save alexrepty/d7a9a9b7c10b8f8d75e8 to your computer and use it in GitHub Desktop.
One way to use a custom reorder control in iOS 8 apps.
- (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];
}
@alexrepty
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment