我在程序开发时发现,将UINavigationController+UIScrollView组合起来使用时,我将UIImageView放入UIScrollView时,UIImageView并不是按照我预想的那样,以UIScrollView的顶点作为视图添加的位置,这是由于UINavigationController在添加了导航栏以后,将会以导航栏下方作为UIScrollView的原点位置,有时候我们想完全显示该视图的内容,解决方法,看下面这个程序
#import "RootViewController.h" @interface RootViewController () @property (retain,nonatomic,readwrite)UIScrollView * scrollView; @end @implementation RootViewController #define HIGHTOFNVBAR 64 #define HIGHTOFTOOLBAR 44 - (instancetype)init { self = [super init]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self.navigationController setNavigationBarHidden:NO animated:NO]; [self.navigationController setToolbarHidden:NO animated:NO]; self.view.backgroundColor = [UIColor blackColor]; _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; _scrollView.contentSize = CGSizeMake(960, 0); _scrollView.backgroundColor = [UIColor redColor]; [self.view addSubview:_scrollView]; UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; [_scrollView addGestureRecognizer:tapGesture]; _scrollView.pagingEnabled = YES; _scrollView.userInteractionEnabled = YES; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.showsVerticalScrollIndicator = NO; UIImageView *p_w_picpathView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; UIImageView *p_w_picpathView2 = [[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 320, 480)]; UIImageView *p_w_picpathView3 = [[UIImageView alloc]initWithFrame:CGRectMake(640, 0, 320, 480)]; p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"IMG_0242.JPG"]; p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"IMG_0243.JPG"]; p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"IMG_0244.JPG"]; _scrollView.contentInset = UIEdgeInsetsMake(-HIGHTOFNVBAR, 0, 0, 0); self.navigationController.toolbar.translucent = YES; self.navigationController.navigationBar.translucent = YES; _scrollView.delegate = self; NSLog(@"%@",NSStringFromCGRect(p_w_picpathView2.frame)); [_scrollView addSubview:p_w_picpathView1]; [_scrollView addSubview:p_w_picpathView2]; [_scrollView addSubview:p_w_picpathView3]; } - (void)tap:(UITapGestureRecognizer *)tap { static int i = 0; if (i%2 == 0) { [self.navigationController setNavigationBarHidden:YES animated:YES]; [self.navigationController setToolbarHidden:YES animated:YES]; _scrollView.contentInset = UIEdgeInsetsMake(0, 0, HIGHTOFTOOLBAR, 0); } else { [self.navigationController setNavigationBarHidden:NO animated:YES]; [self.navigationController setToolbarHidden:NO animated:YES]; _scrollView.contentInset = UIEdgeInsetsMake(0, 0, -HIGHTOFTOOLBAR, 0); } i++; }
使用UIScrollerView的contentInset属性对UIScrollerView进行扩展即可
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。