1

I have a class that provides a custom menu for all its subclasses, but since i started using the text field have run to the keyboard problem so i use the normal method and embed the subclass in a resizing uiscrollview. the problem is that the scrollview doesnt cover the menu that belongs to its super class.

The idea that came to mind is to create a scrollview in the super class and inside the scrollview put the menu. I run it and everything seemed to be okay until i couldn't interact with anything on the screen other than the navigation bar and the menu which lead me to believe that maybe the subclass uielements are not subviews of the scrollview.

The problem even expands since in the subclasses I create uielements in the storyboard and in code. My question would be how to make that all uielements created in the subclass and added in self.view become subviews of the superclass' scrollview.

SUPERCLASS

- (void)viewDidLoad
{
    [super viewDidLoad];


    //_scrollViewMain = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    //[self.view addSubview:_scrollViewMain];

    _slideButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(slide)];
    [self.navigationItem setRightBarButtonItem:_slideButton];

    _menuView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - 44, self.view.bounds.size.width, 44)];
    _menuView.backgroundColor = [UIColor colorWithRed:63.0/255.0 green:63.0/255.0 blue:63.0/255.0 alpha:1.0];
    [self.view addSubview:_menuView];
    //[_scrollViewMain addSubview:_menuView];

    UIView *redFrameView = [[UIView alloc] initWithFrame:CGRectMake(0, _menuView.frame.size.height / 2, _menuView.frame.size.width, _menuView.frame.size.height / 2)];
    [redFrameView setBackgroundColor:[UIColor colorWithRed:170.0/255.0 green:0.0 blue:0.0 alpha:0.5]];
    [_menuView addSubview:redFrameView];

    _menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_menuButton setImage:[UIImage imageNamed:@"19-gear.png"] forState:UIControlStateNormal];
    [_menuButton addTarget:self action:@selector(menu) forControlEvents:UIControlEventTouchUpInside];
    _menuButton.frame = CGRectMake(16.0, 9.0, 26.0, 26.0);

    _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addButton setImage:[UIImage imageNamed:@"13-plus.png"] forState:UIControlStateNormal];
    [_addButton addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    _addButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
    _addButton.userInteractionEnabled = NO;
    _addButton.alpha = 0.0;

    _editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_editButton setImage:[UIImage imageNamed:@"187-pencil.png"] forState:UIControlStateNormal];
    [_editButton addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    _editButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
    _editButton.userInteractionEnabled = NO;
    _editButton.alpha = 0.0;

    _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_deleteButton setImage:[UIImage imageNamed:@"11-x.png"] forState:UIControlStateNormal];
    [_deleteButton addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    _deleteButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
    _deleteButton.userInteractionEnabled = NO;
    _deleteButton.alpha = 0.0;

    _downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_downloadButton setImage:[UIImage imageNamed:@"57-download.png"] forState:UIControlStateNormal];
    [_downloadButton addTarget:self action:@selector(download) forControlEvents:UIControlEventTouchUpInside];
    _downloadButton.frame = CGRectMake(16.0, 15.0, 11.0, 14.0);
    _downloadButton.userInteractionEnabled = NO;
    _downloadButton.alpha = 0.0;

    _uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_uploadButton setImage:[UIImage imageNamed:@"56-cloud.png"] forState:UIControlStateNormal];
    [_uploadButton addTarget:self action:@selector(upload) forControlEvents:UIControlEventTouchUpInside];
    _uploadButton.frame = CGRectMake(16.0, 15.0, 21.0, 14.0);
    _uploadButton.userInteractionEnabled = NO;
    _uploadButton.alpha = 0.0;

    _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_shareButton setImage:[UIImage imageNamed:@"124-bullhorn.png"] forState:UIControlStateNormal];
    [_shareButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    _shareButton.frame = CGRectMake(16.0, 15.0, 21.0, 14.0);
    _shareButton.userInteractionEnabled = NO;
    _shareButton.alpha = 0.0;


    [_menuView addSubview:_addButton];
    [_menuView addSubview:_editButton];
    [_menuView addSubview:_deleteButton];
    [_menuView addSubview:_downloadButton];
    [_menuView addSubview:_uploadButton];
    [_menuView addSubview:_shareButton];
    [_menuView addSubview:_menuButton];

    _isMenuVisible = NO;
}

FIRST attempt but the scrolldidn't included the menuview in the superclass

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self registerForKeyboardNotifications];

    self.title = [_exercise valueForKey:kExerciseName];

    [super ableAdd];
    [super ableDownload];
    [super ableShare];
    if ([[_exercise valueForKey:kExerciseIsDefault] boolValue]) {
        [super ableUpload];
        [super ableDelete];
    }

    _doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];

    _nameLabel.text = [_exercise valueForKey:kExerciseName];
    _nameLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
    _categoryLabel.text = [[_exercise valueForKey:kExerciseEType] valueForKey:kExerciseTypeName];
    _categoryLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
    _gearLabel.text = [[_exercise valueForKey:kGear] valueForKey:kGearName];
    _gearLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];

    _addRecordButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 30) / 2, 7, 30, 30)];
    [_addRecordButton setImage:[UIImage imageNamed:@"85-trophy.png"] forState:UIControlStateNormal];
    [_addRecordButton addTarget:self action:@selector(addRecord) forControlEvents:UIControlEventTouchUpInside];
    [_toolView addSubview:_addRecordButton];

    _showChartButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 7, 30, 30)];
    [_showChartButton setImage:[UIImage imageNamed:@"16-line-chart.png.png"] forState:UIControlStateNormal];
    [_showChartButton addTarget:self action:@selector(showChart) forControlEvents:UIControlEventTouchUpInside];
    [_toolView addSubview:_showChartButton];

    _numberOfPages = 3;
    _currentPage = 1;

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 174, 320, self.view.bounds.size.height - 262)];
    _scrollView.pagingEnabled = YES;
    _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * _numberOfPages, _scrollView.bounds.size.height);
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = NO;

    _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
    _textView.contentSize = CGSizeMake(_scrollView.bounds.size.width, _scrollView.bounds.size.height);
    _textView.text = [_exercise valueForKey:kExerciseOverview];
    _textView.editable = NO;

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * 1, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
    _tableView.contentSize = CGSizeMake(_scrollView.bounds.size.width, _scrollView.bounds.size.height);
    _tableView.dataSource = self;
    _tableView.delegate = self;

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                               initWithTarget:self action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 2.0;
    longPress.delegate = (id<UIGestureRecognizerDelegate>)self;
    [_tableView addGestureRecognizer:longPress];

    _movieView = [[UIWebView alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * 2, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
    _movieView.opaque = YES;
    _movieView.backgroundColor = [UIColor whiteColor];
    _movieView.autoresizesSubviews = YES;
    [_movieView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[_exercise valueForKey:kExerciseUrlAddress]]]];


    [_scrollView addSubview:_textView];
    [_scrollView addSubview:_tableView];
    [_scrollView addSubview:_movieView];
    [_scrollView scrollRectToVisible:CGRectMake(_scrollView.bounds.size.width * 1, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height) animated:NO];

    [_scrollViewMain addSubview:_scrollView];
    //[self.view addSubview:_scrollView];

    _pageControl.numberOfPages = _numberOfPages;
    _pageControl.currentPage = _currentPage;

    _records = [[NSMutableArray alloc] init];
    _records = [[NSArray arrayWithArray:[[_exercise valueForKey:kExerciseERecords] allObjects]] mutableCopy];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:kExerciseRecordRecordDate ascending:NO];
    [_records sortUsingDescriptors:[NSArray arrayWithObject:sort]];
}

- (void)viewDidDisappear:(BOOL)animated
{
    if (self.editing) {
        [super setPan];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];

    [super setPan];

    if (self.navigationItem.rightBarButtonItem == nil) {
        self.navigationItem.rightBarButtonItem = _doneButton;
    }

    _addRecordButton.enabled = !editing;
    _showChartButton.enabled = !editing;

    if (![[_exercise valueForKey:kExerciseIsDefault] boolValue]) {
        [super ableUpload];
        [super ableDelete];
        _textView.editable = editing;
    }

}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    _scrollViewMain.contentInset = contentInsets;
    _scrollViewMain.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, _activeField.frame.origin.y-kbSize.height);
        [_scrollViewMain setContentOffset:scrollPoint animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    _scrollViewMain.contentInset = contentInsets;
    _scrollViewMain.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    _activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _activeField = nil;
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jake Ortiz
  • 503
  • 9
  • 20
  • could you provide the code for your classes? just from your description it's a little hard to figure out. Although your class design sounds a bit odd. – Tobi Apr 07 '13 at 18:11
  • I edited it using one of the subclass examples where i create a textview and when i try to edit it it all goes well but the scrollview main which is made in IB doesnt scroll the superclass menu that is on the bottom of the view. – Jake Ortiz Apr 07 '13 at 18:38

0 Answers0