//// ViewController.m// 實訓一練習//// Created by 員非凡 on 2018/1/17.// Copyright ? 2018年 yff. All rights reserved.//#import "ViewController.h"#import "SGPagingView.h"#import "oneViewController.h"#import "twoViewController.h"#import "threeViewController.h"#import "fourViewController.h"#import "fiveViewController.h"#import "sixViewController.h"#import "caidanViewController.h"@interface ViewController ()@property (nonatomic, strong) SGPageTitleView *pageTitleView;
@property (nonatomic, strong) SGPageContentView *pageContentView;
@property (nonatomic, assign) BOOL isPushNextVC;
@property (nonatomic,strong) UIButton *jaihaobutton;
@property (nonatomic,strong) caidanViewController *itemPopVC;
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view, typically from a nib.
? ? self.view.backgroundColor = [UIColor whiteColor];
? ? self.jaihaobutton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, 60, 40, 40)];
? ? //self.jaihaobutton.backgroundColor = [UIColor redColor];
? ? [self.jaihaobutton setTitle:@"+" forState:UIControlStateNormal];
? ? [self.jaihaobutton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
? ? [self.jaihaobutton addTarget:self action:@selector(jiahao) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:self.jaihaobutton];
? ? [self setupNavigationBar];
}
-(void)jiahao{
? ? //初始化 VC
? ? self.itemPopVC = [[caidanViewController alloc] init];
? ? //設置 VC 彈出方式
? ? self.itemPopVC.modalPresentationStyle = UIModalPresentationPopover;
? ? //設置依附的按鈕
? ? self.itemPopVC.popoverPresentationController.sourceView = self.jaihaobutton;
? ? //可以指示小箭頭顏色
? ? self.itemPopVC.popoverPresentationController.backgroundColor = [UIColor grayColor];
? ? //箭頭方向
? ? self.itemPopVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
? ? // 指定箭頭所指區域的矩形框范圍(位置和尺寸),以sourceView的左上角為坐標原點
? ? // 這個可以 通過 Point 或? Size 調試位置
? ? self.itemPopVC.popoverPresentationController.sourceRect = self.jaihaobutton.bounds;
? ? //代理
? ? self.itemPopVC.popoverPresentationController.delegate = self;
? ? [self presentViewController:self.itemPopVC animated:YES completion:nil];
? ? //content尺寸
? ? //self.itemPopVC.preferredContentSize = CGSizeMake(400, 400);
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
? ? return UIModalPresentationNone;
}
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
? ? return YES;? //點擊蒙版popover消失, 默認YES
}
- (void)setupNavigationBar {
? ? NSArray *titleArr = [@[@"移動通訊", @"傳媒", @"網工", @"軟工", @"云計算", @"建筑", ]mutableCopy];
? ? SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
? ? /// pageTitleView
? ? // 這里的 - 10 是為了讓 SGPageTitleView 超出父視圖,給用戶一種效果體驗
? ? self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 60, self.view.frame.size.width-40? ? , 44) delegate:self titleNames:titleArr configure:configure];
? ? //_pageTitleView.backgroundColor = [UIColor yellowColor];
? ? [self.view addSubview:_pageTitleView];
? ? oneViewController *oneVC = [[oneViewController alloc] init];
? ? twoViewController *twoVC = [[twoViewController alloc] init];
? ? threeViewController *threeVC = [[threeViewController alloc] init];
? ? fourViewController *fourVC = [[fourViewController alloc] init];
? ? fiveViewController *fiveVC = [[fiveViewController alloc] init];
? ? sixViewController *sixVC = [[sixViewController alloc] init];
? ? NSArray *childArr = @[oneVC, twoVC, threeVC, fourVC, fiveVC, sixVC];
? ? /// pageContentView
? ? CGFloat contentViewHeight = self.view.frame.size.height - 104;
? ? self.pageContentView = [[SGPageContentView alloc] initWithFrame:CGRectMake(0, 104, self.view.frame.size.width, contentViewHeight) parentVC:self childVCs:childArr];
? ? _pageContentView.delegatePageContentView = self;
? ? [self.view addSubview:_pageContentView];
}
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
? ? [self.pageContentView setPageCententViewCurrentIndex:selectedIndex];
}
- (void)pageContentView:(SGPageContentView *)pageContentView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex {
? ? [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
}
@end
//// caidanViewController.m// 實訓一練習//// Created by 員非凡 on 2018/1/17.// Copyright ? 2018年 yff. All rights reserved.//#import "caidanViewController.h"@interface caidanViewController (){
? ? NSMutableArray *_addArray;
}
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation caidanViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view.
? ? self.view.backgroundColor = [UIColor whiteColor];
? ? [self setupView];
}
#pragma mark 創建 tableView
- (void)setupView {
? ? self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
? self. tableView.dataSource = self;
? ? self.tableView.delegate = self;
? ? self.tableView.scrollEnabled = NO;
? ? self.tableView.backgroundColor = [UIColor grayColor];
? ? [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
? ? [self.view addSubview:self.tableView];
? ? //? ? [self.tableView setSeparatorColor:[UIColor whiteColor]];
? ? _addArray = [NSMutableArray arrayWithObjects:@"確認添加",@"刪除添加",@"關閉", nil];
}
#pragma mark tabelView 協議方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? return _addArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
? ? cell.backgroundColor = [UIColor grayColor];
? ? //? ? cell.textLabel.textAlignment = NSTextAlignmentCenter;
? ? cell.textLabel.text = _addArray[indexPath.row];
? ? cell.textLabel.textColor = [UIColor whiteColor];
? ? return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
? ? NSLog(@"%zd",indexPath.row);
? ? // 初始化提示框控制器
? ? UIAlertController *Alc = [UIAlertController alertControllerWithTitle:@"確認添加" message:@"操作已完成" preferredStyle:UIAlertControllerStyleAlert];
? ? // 初始化? 提示框上的按鈕
? ? UIAlertAction *AcOne? = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? [self.tableView reloadData];
? ? }];
? ? // 將按鈕添加到控制器上
? ? [Alc addAction:AcOne];
? ? // 展示提示框
? ? [self presentViewController:Alc animated:YES completion:nil];
}
- (CGSize)preferredContentSize {
? ? if (self.presentingViewController && self.tableView != nil) {
? ? ? ? CGSize tempSize = self.presentingViewController.view.bounds.size;
? ? ? ? tempSize.width = 150;
? ? ? ? //返回一個完美適應tableView的大小的 size; sizeThatFits 返回的是最合適的尺寸, 但不會改變控件的大小
? ? ? ? CGSize size = [self.tableView sizeThatFits:tempSize];
? ? ? ? return size;
? ? }else{
? ? ? ? return [self preferredContentSize];
? ? }
}
@end
//// caidanViewController.m// 實訓一練習//// Created by 員非凡 on 2018/1/17.// Copyright ? 2018年 yff. All rights reserved.//#import "caidanViewController.h"@interface caidanViewController (){
? ? NSMutableArray *_addArray;
}
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation caidanViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view.
? ? self.view.backgroundColor = [UIColor whiteColor];
? ? [self setupView];
}
#pragma mark 創建 tableView
- (void)setupView {
? ? self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
? self. tableView.dataSource = self;
? ? self.tableView.delegate = self;
? ? self.tableView.scrollEnabled = NO;
? ? self.tableView.backgroundColor = [UIColor grayColor];
? ? [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
? ? [self.view addSubview:self.tableView];
? ? //? ? [self.tableView setSeparatorColor:[UIColor whiteColor]];
? ? _addArray = [NSMutableArray arrayWithObjects:@"確認添加",@"刪除添加",@"關閉", nil];
}
#pragma mark tabelView 協議方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? return _addArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
? ? cell.backgroundColor = [UIColor grayColor];
? ? //? ? cell.textLabel.textAlignment = NSTextAlignmentCenter;
? ? cell.textLabel.text = _addArray[indexPath.row];
? ? cell.textLabel.textColor = [UIColor whiteColor];
? ? return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
? ? NSLog(@"%zd",indexPath.row);
? ? // 初始化提示框控制器
? ? UIAlertController *Alc = [UIAlertController alertControllerWithTitle:@"確認添加" message:@"操作已完成" preferredStyle:UIAlertControllerStyleAlert];
? ? // 初始化? 提示框上的按鈕
? ? UIAlertAction *AcOne? = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? [self.tableView reloadData];
? ? }];
? ? // 將按鈕添加到控制器上
? ? [Alc addAction:AcOne];
? ? // 展示提示框
? ? [self presentViewController:Alc animated:YES completion:nil];
}
- (CGSize)preferredContentSize {
? ? if (self.presentingViewController && self.tableView != nil) {
? ? ? ? CGSize tempSize = self.presentingViewController.view.bounds.size;
? ? ? ? tempSize.width = 150;
? ? ? ? //返回一個完美適應tableView的大小的 size; sizeThatFits 返回的是最合適的尺寸, 但不會改變控件的大小
? ? ? ? CGSize size = [self.tableView sizeThatFits:tempSize];
? ? ? ? return size;
? ? }else{
? ? ? ? return [self preferredContentSize];
? ? }
}
@end