FLEXFieldEditorViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
  38. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  39. scrollInsets.bottom = keyboardSize.height;
  40. self.scrollView.contentInset = scrollInsets;
  41. self.scrollView.scrollIndicatorInsets = scrollInsets;
  42. // Find the active input view and scroll to make sure it's visible.
  43. for (FLEXArgumentInputView *argumentInputView in self.fieldEditorView.argumentInputViews) {
  44. if (argumentInputView.inputViewIsFirstResponder) {
  45. CGRect scrollToVisibleRect = [self.scrollView convertRect:argumentInputView.bounds fromView:argumentInputView];
  46. [self.scrollView scrollRectToVisible:scrollToVisibleRect animated:YES];
  47. break;
  48. }
  49. }
  50. }
  51. - (void)keyboardWillHide:(NSNotification *)notification
  52. {
  53. UIEdgeInsets scrollInsets = self.scrollView.contentInset;
  54. scrollInsets.bottom = 0.0;
  55. self.scrollView.contentInset = scrollInsets;
  56. self.scrollView.scrollIndicatorInsets = scrollInsets;
  57. }
  58. - (BOOL)prefersStatusBarHidden
  59. {
  60. return YES;
  61. }
  62. - (void)viewDidLoad
  63. {
  64. [super viewDidLoad];
  65. self.view.backgroundColor = [FLEXUtility scrollViewGrayColor];
  66. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  67. self.scrollView.backgroundColor = self.view.backgroundColor;
  68. self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  69. self.scrollView.delegate = self;
  70. [self.view addSubview:self.scrollView];
  71. self.fieldEditorView = [[FLEXFieldEditorView alloc] init];
  72. self.fieldEditorView.backgroundColor = self.view.backgroundColor;
  73. self.fieldEditorView.targetDescription = [NSString stringWithFormat:@"%@ %p", [self.target class], self.target];
  74. [self.scrollView addSubview:self.fieldEditorView];
  75. self.setterButton = [[UIBarButtonItem alloc] initWithTitle:[self titleForActionButton] style:UIBarButtonItemStyleDone target:self action:@selector(actionButtonPressed:)];
  76. self.navigationItem.rightBarButtonItem = self.setterButton;
  77. }
  78. - (void)viewWillLayoutSubviews
  79. {
  80. CGSize constrainSize = CGSizeMake(self.scrollView.bounds.size.width, CGFLOAT_MAX);
  81. CGSize fieldEditorSize = [self.fieldEditorView sizeThatFits:constrainSize];
  82. self.fieldEditorView.frame = CGRectMake(0, 0, fieldEditorSize.width, fieldEditorSize.height);
  83. self.scrollView.contentSize = fieldEditorSize;
  84. }
  85. - (FLEXArgumentInputView *)firstInputView
  86. {
  87. return [[self.fieldEditorView argumentInputViews] firstObject];
  88. }
  89. - (void)actionButtonPressed:(id)sender
  90. {
  91. // Subclasses can override
  92. [self.fieldEditorView endEditing:YES];
  93. }
  94. - (NSString *)titleForActionButton
  95. {
  96. // Subclasses can override.
  97. return @"Set";
  98. }
  99. @end