| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- //
- // MOForbidScreenShotView.m
- // MiMoLive
- //
- // Created by SuperC on 2024/4/12.
- //
- #import "MOForbidScreenShotView.h"
- @interface MOForbidScreenShotView ()<UITextFieldDelegate>
- @property (nonatomic, strong) UITextField *textField;
- @property (nonatomic, strong) UIView *safeZone;
- @end
- @implementation MOForbidScreenShotView
- + (UIView *)creactWithFrame:(CGRect)frame {
- return [[MOForbidScreenShotView alloc] initWithFrame:frame];
- }
- - (instancetype)initWithCoder:(NSCoder *)coder {
- self = [super initWithCoder:coder];
- if (self) {
- [self setupUI];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI {
- [self addSubview:self.safeZone];
-
- UILayoutPriority lowPriority = UILayoutPriorityDefaultLow - 1;
- UILayoutPriority heightPriority = UILayoutPriorityDefaultHigh - 1;
- self.safeZone.translatesAutoresizingMaskIntoConstraints = NO;
- [self.safeZone setContentHuggingPriority:lowPriority forAxis:UILayoutConstraintAxisVertical];
- [self.safeZone setContentHuggingPriority:lowPriority forAxis:UILayoutConstraintAxisHorizontal];
- [self.safeZone setContentCompressionResistancePriority:heightPriority forAxis:UILayoutConstraintAxisVertical];
- [self.safeZone setContentCompressionResistancePriority:heightPriority forAxis:UILayoutConstraintAxisHorizontal];
-
- [self addConstraints:@[
- [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0],
- [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
- [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:0],
- [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0],
- ]];
-
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
- }
- - (void)addSubview:(UIView *)view {
- if (self.safeZone == view) {
- [super addSubview:view];
- }else{
- [self.safeZone addSubview:view];
- }
- }
- - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index {
- if (self.safeZone == view) {
- [super insertSubview:view atIndex:index];
- }else{
- [self.safeZone insertSubview:view atIndex:index];
- }
- }
- - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview {
- if (self.safeZone == view) {
- [super insertSubview:view aboveSubview:siblingSubview];
- }else{
- [self.safeZone insertSubview:view aboveSubview:siblingSubview];
- }
- }
- - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview {
- if (self.safeZone == view) {
- [super insertSubview:view belowSubview:siblingSubview];
- }else{
- [self.safeZone insertSubview:view belowSubview:siblingSubview];
- }
- }
- - (void)keyboardWillShow:(NSNotification *)noti {
- if (self.textField.isFirstResponder) {
- [self.textField resignFirstResponder];
- self.textField.subviews.firstObject.userInteractionEnabled = YES;
- }
- }
- #pragma mark - UITextFieldDelegate
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
- return NO;
- }
- - (UITextField *)textField{
- if(!_textField){
- _textField = [[UITextField alloc]init];
- _textField.secureTextEntry = YES;
- _textField.enabled = NO;
- _textField.delegate = self;
- }
- return _textField;
- }
- - (UIView *)safeZone{
- if(!_safeZone){
- _safeZone = self.textField.subviews.firstObject ?: [[UIView alloc] init];
- _safeZone.userInteractionEnabled = YES;
- for (UIView *v in _safeZone.subviews) {
- [v removeFromSuperview];
- }
- }
- return _safeZone;
- }
- @end
|