ios screen capture-related functionality inhert viewcontroller invisible

When working with screen capture-related functionality, I want to create a BaseViewController to provide functionality for other UIViewControllers. However, I have set up the loadView method in BaseViewController and provided a property to allow UIViewController instances to customize it independently.

- (void)loadView {
NSLog(@"BaseViewController loadView");
if (self.banScreenShot) {
    UITextField *textField = [[UITextField alloc] init];
    textField.secureTextEntry = YES;
    textField.enabled = NO;
    if (textField.subviews.firstObject != nil) {
        self.view = textField.subviews.firstObject;
    } else {
        self.view = [[UIView alloc] init];
    }
} else {
    self.view = [[UIView alloc] init];
}
self.view.userInteractionEnabled = YES;
self.view.frame = CGRectMake(0, 0,
                             [UIScreen mainScreen].bounds.size.width,
                             [UIScreen mainScreen].bounds.size.height);
self.view.backgroundColor = [UIColor whiteColor];
}

When I set up some components in the storyboard and inherit from BaseViewController in MainViewController, setting

 self.banScreenShot = YES;

the components in the storyboard become invisible, and the view becomes entirely white. How can I allow MainViewController to have its own display functionality while still inheriting the loadView functionality from BaseViewController?
Thank you.

MainViewController header file:

 #import "BaseViewController.h"

 @interface MainViewController : BaseViewController
 @end

.m file

 - (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"ViewController viewDidLoad");
self.banScreenShot = YES;
 }

  • You should be aware that the secure text fileld hack to prevent screen shots no longer works on iOS 17

    – 

  • Also, your code doesn’t make sense; you have implemented functionality in loadView but you are setting the property in viewDidLoad, which runs after loadView.

    – 




  • How are you adding all of the views for MainViewController? As you have implemented loadView you are taking full responsibility for the view loading process. You probably want to add the pointless text field in viewDidLoad

    – 




  • MainViewController is bind in the storyboard init viewcontroller class,my storyboard have more ui elements ,but when I set the inhert BaseViewcontroller, the all elements is invisible.

    – 

  • From the loadView documentation If you use Interface Builder to create your views and initialize the view controller, you must not override this method. – As per my previous comment, you probably want to add the view in viewDidLoad of your superclass, not in loadView

    – 

Leave a Comment