开始时候的效果图:
点击增加时候的效果图:
点击减少时候的效果图:
ReaderViewController.h
#import <UIKit/UIKit.h> @interface ReaderViewController : UIViewController <UIScrollViewDelegate> { UIScrollView *scrollView; } @property(nonatomic,strong) UIScrollView *scrollView;; -(void)add; -(void)dele; -(void)reloadView; @end
ReaderViewController.m
#import "ReaderViewController.h" #define ImageHeight 120 static int num = 0; @interface ReaderViewController () @end @implementation ReaderViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //字体的颜色 self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:248.0/255.0 green:172.0/255.0 blue:37.0/255.0 alpha:1.0]; //导航栏背景图 [self addBackgroundView:@"1.png"]; //左侧删除按钮 UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"delete" style:UIBarButtonItemStyleBordered target:self action:@selector(dele)]; //右侧增加按钮 UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)]; self.navigationItem.rightBarButtonItem = rightBarButton; self.navigationItem.leftBarButtonItem = leftBarButton; //scrollerView scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)]; scrollView.contentSize=CGSizeMake(320,480); //设置背景颜色 UIImage *backimg=[UIImage imageNamed:@"3.png"]; UIImageView *backview=[[UIImageView alloc] initWithImage:backimg]; backview.frame=CGRectMake(0, 0, 320, 480); [self.view addSubview:backview]; [self.view addSubview:scrollView]; //重新刷新界面 [self reloadView]; } //导航栏上标题 -(void)addBackgroundView:(NSString*)image{ UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]]; imgView.frame = CGRectMake(0, 0, 320, 44); [self.navigationController.navigationBar addSubview: imgView]; UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake((320-150)/2, 0, 150, 44)]; [title setText:@"书架"]; [title setTextColor:[UIColor whiteColor]]; [title setTextAlignment:NSTextAlignmentCenter]; [title setShadowColor:[UIColor grayColor]]; [title setBackgroundColor:[UIColor clearColor]]; [title setFont:[UIFont boldSystemFontOfSize:20.0]]; [self.navigationController.navigationBar addSubview:title]; } //添加一个书架 -(void)add{ num++; UIImage *backimg=[UIImage imageNamed:@"2.png"]; UIImageView *backview=[[UIImageView alloc] initWithImage:backimg]; backview.frame=CGRectMake(0, (num-1)*ImageHeight, 320, ImageHeight); backview.tag = num+100; [scrollView addSubview:backview]; if (num>3) { scrollView.contentSize=CGSizeMake(320,num*ImageHeight); } } //删除一个书架 -(void)dele{ if (num<=0) { return; } for (UIView *view in scrollView.subviews) { if(view.tag == (num+100)) [view removeFromSuperview]; } num--; if (num>3) { scrollView.contentSize=CGSizeMake(320,num*ImageHeight); } } //主界面初始化 -(void)reloadView{ NSLog(@"reloadView"); //遍历当前界面的所有子界面,把子界面删除干净 for(UIView *view in scrollView.subviews){ [view removeFromSuperview]; } scrollView.showsVerticalScrollIndicator=NO; scrollView.showsHorizontalScrollIndicator=NO; scrollView.delegate=self; [scrollView setScrollEnabled:YES]; }