1

我有一个为其所有子类提供自定义菜单的类,但是由于我开始使用文本字段已经遇到键盘问题,所以我使用普通方法并将子类嵌入到调整大小的 uiscrollview 中。问题是滚动视图没有覆盖属于其超类的菜单。

想到的想法是在超类中创建一个滚动视图,并在滚动视图中放置菜单。我运行它,一切似乎都很好,直到我无法与屏幕上除了导航栏和菜单之外的任何东西进行交互,这让我相信子类 uielements 可能不是滚动视图的子视图。

问题甚至扩大了,因为在子类中我在情节提要和代码中创建了 uielements。我的问题是如何使在子类中创建并添加到 self.view 中的所有 uielements 成为超类滚动视图的子视图。

超一流

- (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;
}

第一次尝试,但滚动没有在超类中包含菜单视图

- (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;
}
4

0 回答 0