我正在开发一个iOS目标C项目,我想在我的应用程序中使用导航抽屉。我不想使用任何第三方库。我正在自己开发所有代码。
这些是我已经完成的First或HomeViewController的一些屏幕截图。
当我点击菜单按钮时,我的抽屉(嵌入Second或NavigationTableViewController的容器视图)成功打开。但我无法从Home View Controller滚动或访问表格元素。
请帮忙。
我也在这里发布我的代码。
HomeViewController.m
#import "HomeViewController.h"
#import "HomeCollectionViewItemCell.h"
#import "NavigationTableViewController.h"
#define kCellsPerRow 2
@interface HomeViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *viewNavigationPopUp;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *collectionViewFlowLayout;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewSlider;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControlSlider;
@property (nonatomic) NSInteger sliderImageCount;
@end
@implementation HomeViewController
#pragma mark - View controller life cycle methods
- (void)viewDidLoad {
[super viewDidLoad];
[self setSliderImageCount:0];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
[self.imageViewSlider setAnimationImages:@[[UIImage imageNamed:@"slider_img1.jpg"], [UIImage imageNamed:@"slider_img2.jpg"], [UIImage imageNamed:@"slider_img3.jpg"], [UIImage imageNamed:@"slider_img4.jpg"]]];
//[self.imageViewSlider setImage:[self.imageViewSlider.animationImages objectAtIndex:self.sliderImageCount]];
[self.pageControlSlider setCurrentPage:self.sliderImageCount];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageSliderSwipe:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.imageViewSlider addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageSliderSwipe:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.imageViewSlider addGestureRecognizer:swipeRight];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGFloat length = (self.collectionView.frame.size.width - self.collectionViewFlowLayout.sectionInset.left - self.collectionViewFlowLayout.sectionInset.right - (self.collectionViewFlowLayout.minimumInteritemSpacing * (kCellsPerRow - 1))) / kCellsPerRow;
[self.collectionViewFlowLayout setItemSize:CGSizeMake(length, length)];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
#pragma mark - Collection view methods
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HomeCollectionViewItemCell *homeCollectionViewItemCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewItemCell" forIndexPath:indexPath];
return homeCollectionViewItemCell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
#pragma mark - button acrion methods
- (IBAction)buttonToggleDrawerMenuAction:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
CGRect newViewFrame = [self.view frame];
newViewFrame.origin.x = [self.view frame].origin.x > 0.0 ? 0.0 : [[UIScreen mainScreen] bounds].size.width * 0.797;
[self.view setFrame:newViewFrame];
[UIView commitAnimations];
}
- (void)handleImageSliderSwipe:(UISwipeGestureRecognizer *)swipe {
[self setSliderImageCount:[swipe direction] == UISwipeGestureRecognizerDirectionLeft ? (self.sliderImageCount + 1) : (self.sliderImageCount - 1)];
[self setSliderImageCount:(self.sliderImageCount < 0) ? 3 : self.sliderImageCount];
[self setSliderImageCount:(self.sliderImageCount > 3) ? 0 : self.sliderImageCount];
[self.imageViewSlider setImage:[self.imageViewSlider.animationImages objectAtIndex:self.sliderImageCount]];
[self.pageControlSlider setCurrentPage:self.sliderImageCount];
}
#pragma mark - other
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"EmbedSegue"]) {
NavigationTableViewController *navigationTableViewController = (NavigationTableViewController *)[segue destinationViewController];
[navigationTableViewController setHomeViewController:self];
}
}
@end
NavigationTableViewController.m
#import "NavigationTableViewController.h"
@interface NavigationTableViewController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *navigationTableView;
@end
@implementation NavigationTableViewController
#pragma mark - View controller life cycle methods
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationTableView setDataSource:self];
[self.navigationTableView setDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
#pragma mark - table view methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NavigationTableViewCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Item - %ld", (long)indexPath.row];
return cell;
}
#pragma mark - other
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
@end
NavigationTableViewController.h
#import <UIKit/UIKit.h>
@class HomeViewController;
@interface NavigationTableViewController : UIViewController
@property (weak, nonatomic) HomeViewController *homeViewController;
@end
更新
我有一些线索,但没有得到为什么会发生这种情况?实际上我的代码是正确的,但由于自动布局限制,我遇到了这个问题。
假设我的视图控制器视图的框架是(0,0,414,736),容器框架是(-330,0,330,736)。 我的自动布局有以下限制:
容器高度= [viewController视图] .size.height
容器宽度= [viewController视图] .size.width * 0.8 // 0是常量,乘数是0.8
容器尾随= [viewController视图]前导+ 0; // 0是常数,乘数是1
容器顶部= [viewController视图]顶部布局指南,即0
此时我可以滚动以及在表格视图中选择项目。但是当我按下菜单按钮时,我的容器框架发生了变化,变成了(0,0,330,736)。现在当我的容器框架发生变化时,我无法滚动或选择tableview内的项目。
基本上我的意思是如果您要在运行时更改容器框架,它将不允许访问嵌入其中的任何视图组件。
如果有人知道这个问题的补救措施,那么请告诉我。 任何帮助表示赞赏。提前致谢:)