FLEXFieldEditorViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // FLEXFieldEditorViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/16/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXFieldEditorViewController.h"
  9. #import "FLEXFieldEditorView.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXUtility.h"
  12. #import "FLEXArgumentInputView.h"
  13. #import "FLEXArgumentInputViewFactory.h"
  14. @interface FLEXFieldEditorViewController () <UIScrollViewDelegate>
  15. @property (nonatomic, strong) UIScrollView *scrollView;
  16. @property (nonatomic, strong, readwrite) id target;
  17. @property (nonatomic, strong, readwrite) FLEXFieldEditorView *fieldEditorView;
  18. @property (nonatomic, strong, readwrite) UIBarButtonItem *setterButton;
  19. @end
  20. @implementation FLEXFieldEditorViewController
  21. - (id)initWithTarget:(id)target
  22. {
  23. self = [super initWithNibName:nil bundle:nil];
  24. if (self) {
  25. self.target = target;
  26. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  28. }
  29. return self;
  30. }
  31. - (void)dealloc
  32. {
  33. [[NSNotificationCenter defaultCenter] removeObserver:self];
  34. }
  35. - (void)keyboardDidShow:(NSNotification *)notification
  36. {
  37. CGRect keyboardRectInWindow = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  38. CGSize keyboardSize = [self.view convertRect:keyboardRectInWindow fromView:nil].size;
  39. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  40. scrollInsets.bottom = keyboardSize.height;
  41. self.scrollView.contentInset = scrollInsets;
  42. self.scrollView.scrollIndicatorInsets = scrollInsets;
  43. // Find the active input view and scroll to make sure it's visible.
  44. for (FLEXArgumentInputView *argumentInputView in self.fieldEditorView.argumentInputViews) {
  45. if (argumentInputView.inputViewIsFirstResponder) {
  46. CGRect scrollToVisibleRect = [self.scrollView convertRect:argumentInputView.bounds fromView:argumentInputView];
  47. [self.scrollView scrollRectToVisible:scrollToVisibleRect animated:YES];
  48. break;
  49. }
  50. }
  51. }
  52. - (void)keyboardWillHide:(NSNotification *)notification
  53. {
  54. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  55. scrollInsets.bottom = 0.0;
  56. self.scrollView.contentInset = scrollInsets;
  57. self.scrollView.scrollIndicatorInsets = scrollInsets;
  58. }
  59. - (void)viewDidLoad
  60. {
  61. [super viewDidLoad];
  62. self.view.backgroundColor = [FLEXUtility scrollViewGrayColor];
  63. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  64. self.scrollView.backgroundColor = self.view.backgroundColor;
  65. self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  66. self.scrollView.delegate = self;
  67. [self.view addSubview:self.scrollView];
  68. self.fieldEditorView = [[FLEXFieldEditorView alloc] init];
  69. self.fieldEditorView.backgroundColor = self.view.backgroundColor;
  70. self.fieldEditorView.targetDescription = [NSString stringWithFormat:@"%@ %p", [self.target class], self.target];
  71. [self.scrollView addSubview:self.fieldEditorView];
  72. self.setterButton = [[UIBarButtonItem alloc] initWithTitle:[self titleForActionButton] style:UIBarButtonItemStyleDone target:self action:@selector(actionButtonPressed:)];
  73. self.navigationItem.rightBarButtonItem = self.setterButton;
  74. }
  75. - (void)viewWillLayoutSubviews
  76. {
  77. CGSize constrainSize = CGSizeMake(self.scrollView.bounds.size.width, CGFLOAT_MAX);
  78. CGSize fieldEditorSize = [self.fieldEditorView sizeThatFits:constrainSize];
  79. self.fieldEditorView.frame = CGRectMake(0, 0, fieldEditorSize.width, fieldEditorSize.height);
  80. self.scrollView.contentSize = fieldEditorSize;
  81. }
  82. - (FLEXArgumentInputView *)firstInputView
  83. {
  84. return [[self.fieldEditorView argumentInputViews] firstObject];
  85. }
  86. - (void)actionButtonPressed:(id)sender
  87. {
  88. // Subclasses can override
  89. [self.fieldEditorView endEditing:YES];
  90. }
  91. - (NSString *)titleForActionButton
  92. {
  93. // Subclasses can override.
  94. return @"Set";
  95. }
  96. @end