MOForbidScreenShotView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // MOForbidScreenShotView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2024/4/12.
  6. //
  7. #import "MOForbidScreenShotView.h"
  8. @interface MOForbidScreenShotView ()<UITextFieldDelegate>
  9. @property (nonatomic, strong) UITextField *textField;
  10. @property (nonatomic, strong) UIView *safeZone;
  11. @end
  12. @implementation MOForbidScreenShotView
  13. + (UIView *)creactWithFrame:(CGRect)frame {
  14. return [[MOForbidScreenShotView alloc] initWithFrame:frame];
  15. }
  16. - (instancetype)initWithCoder:(NSCoder *)coder {
  17. self = [super initWithCoder:coder];
  18. if (self) {
  19. [self setupUI];
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithFrame:(CGRect)frame {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. [self setupUI];
  27. }
  28. return self;
  29. }
  30. - (void)setupUI {
  31. [self addSubview:self.safeZone];
  32. UILayoutPriority lowPriority = UILayoutPriorityDefaultLow - 1;
  33. UILayoutPriority heightPriority = UILayoutPriorityDefaultHigh - 1;
  34. self.safeZone.translatesAutoresizingMaskIntoConstraints = NO;
  35. [self.safeZone setContentHuggingPriority:lowPriority forAxis:UILayoutConstraintAxisVertical];
  36. [self.safeZone setContentHuggingPriority:lowPriority forAxis:UILayoutConstraintAxisHorizontal];
  37. [self.safeZone setContentCompressionResistancePriority:heightPriority forAxis:UILayoutConstraintAxisVertical];
  38. [self.safeZone setContentCompressionResistancePriority:heightPriority forAxis:UILayoutConstraintAxisHorizontal];
  39. [self addConstraints:@[
  40. [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0],
  41. [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
  42. [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:0],
  43. [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0],
  44. ]];
  45. [[NSNotificationCenter defaultCenter] removeObserver:self];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  47. }
  48. - (void)addSubview:(UIView *)view {
  49. if (self.safeZone == view) {
  50. [super addSubview:view];
  51. }else{
  52. [self.safeZone addSubview:view];
  53. }
  54. }
  55. - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index {
  56. if (self.safeZone == view) {
  57. [super insertSubview:view atIndex:index];
  58. }else{
  59. [self.safeZone insertSubview:view atIndex:index];
  60. }
  61. }
  62. - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview {
  63. if (self.safeZone == view) {
  64. [super insertSubview:view aboveSubview:siblingSubview];
  65. }else{
  66. [self.safeZone insertSubview:view aboveSubview:siblingSubview];
  67. }
  68. }
  69. - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview {
  70. if (self.safeZone == view) {
  71. [super insertSubview:view belowSubview:siblingSubview];
  72. }else{
  73. [self.safeZone insertSubview:view belowSubview:siblingSubview];
  74. }
  75. }
  76. - (void)keyboardWillShow:(NSNotification *)noti {
  77. if (self.textField.isFirstResponder) {
  78. [self.textField resignFirstResponder];
  79. self.textField.subviews.firstObject.userInteractionEnabled = YES;
  80. }
  81. }
  82. #pragma mark - UITextFieldDelegate
  83. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  84. return NO;
  85. }
  86. - (UITextField *)textField{
  87. if(!_textField){
  88. _textField = [[UITextField alloc]init];
  89. _textField.secureTextEntry = YES;
  90. _textField.enabled = NO;
  91. _textField.delegate = self;
  92. }
  93. return _textField;
  94. }
  95. - (UIView *)safeZone{
  96. if(!_safeZone){
  97. _safeZone = self.textField.subviews.firstObject ?: [[UIView alloc] init];
  98. _safeZone.userInteractionEnabled = YES;
  99. for (UIView *v in _safeZone.subviews) {
  100. [v removeFromSuperview];
  101. }
  102. }
  103. return _safeZone;
  104. }
  105. @end