0

I want to insert a view below the viewcontroller.view. My idea is to exchange viewcontroller.view with a tempview at runtime.

In "UIViewController+Ext.m":

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class vc = [UIViewController class];

        SEL originalSEL = @selector(viewDidLoad);
        SEL swizzledSEL = @selector(Easy_viewDidLoad);

        Method originalMethod = class_getInstanceMethod(vc, originalSEL);
        Method swizzledMethod = class_getInstanceMethod(vc, swizzledSEL);

        BOOL didAddMethod =
        class_addMethod(vc, originalSEL, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(vc, swizzledSEL, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        }
        else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    }); 
}

- (void)Easy_viewDidLoad
{
    UIView *view = self.view ;

    UIView *tempV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth(), ScreenHeight())];
    tempV.backgroundColor = [UIColor cyanColor];
    self.view = tempV ;

    view.frame = CGRectMake(0, NavigationHeight(), ScreenWidth(), ScreenHeight()-NavigationHeight());
    [self.view addSubview:view];

    [self Easy_viewDidLoad];
}   

When I add a UITextField on the view controller, the textfield becomes first responder. But the app is killed and throws this error:

-[UIView leftDockItem]: unrecognized selector sent to instance 0x7f91bb742d80

  1. What is the leftDockItem
  2. What is the base way to add a view below viewcontroller.view?
rmaddy
  • 314,917
  • 42
  • 532
  • 579
stack Stevn
  • 114
  • 6
  • `leftDockItem` is a property of `UIKeyboardDockView`; so you must be using that somewhere. Are you using a 3rd party library? – koen Sep 03 '18 at 17:41
  • @Koen think you follow, i have no use UIKeyboardDockView and no use 3rd party library , app killed mast because i have change the viewcontroller.view. – stack Stevn Sep 05 '18 at 07:43
  • You don't need to use swizzling to replace a view. Do you know what line is causing the crash? – koen Sep 05 '18 at 10:55
  • but i need a immobile view below the scrollViewController . crash on uikit fondution . i gress uitextfield need the context , but i thange the controller.view cause the problem. – stack Stevn Sep 05 '18 at 11:09
  • I recommend you enable exception breakpoints in Xcode, this will tell you exactly where the crash happens. See here: https://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode – koen Sep 05 '18 at 12:04
  • i have publish a demo on [link](https://github.com/chenliangloveyou/test) , you run the demo will see the effect. – stack Stevn Sep 05 '18 at 13:04
  • I still don't understand why you are using swizzling for this. You can just add another view during runtime, and use auto layout to position it. – koen Sep 05 '18 at 14:43
  • in UITableViewController . i want add a unmoveview on UITableViewController.view . but unmoveview cann't move when scroll UITableViewController . so i think install a tempview below UITableViewController.view . so i think using swizzling. – stack Stevn Sep 06 '18 at 02:11
  • What is "unmoveview" ? – koen Sep 06 '18 at 11:41
  • in UITableViewController , [self.view addSubView:unmoveview]; this unmoveview will move when scroll the TableView . but i want the unmoveview fobit move. – stack Stevn Sep 06 '18 at 11:47
  • 1
    Maybe this will help: https://stackoverflow.com/questions/18176843/creating-a-uiview-that-sticks-to-bottom-of-uitableview – koen Sep 06 '18 at 11:54
  • @Koen this link finish my problem , unnecessary using swizzling . thank you very much . – stack Stevn Sep 06 '18 at 12:32

0 Answers0