segmentedControl

@interface ViewController ()<UIScrollViewDelegate,UIPopoverPresentationControllerDelegate>{

NSMutableArray *arr;

UIButton *btn;

}

@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) OneViewController *oneVC;

@property (nonatomic, strong) TwoViewController *twoVC;

@property (nonatomic, strong) ThreeViewController *threeVC;

@property (nonatomic, strong) FourViewController *fourVC;

@property (nonatomic, strong) FiveViewController *fiveVC;

@property (nonatomic, strong) SixViewController *sixVC;

@property (nonatomic, strong) UIPopoverPresentationController *pop;

- (void)viewDidLoad?

{

//? ? 創(chuàng)建一個(gè)view,放分段控制器

UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30)];

[self.view addSubview:vv];

//? ? 存儲(chǔ)分段控制器標(biāo)題

arr = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil];

// 適應(yīng)scrollView

self.automaticallyAdjustsScrollViewInsets = NO;

//? ? 創(chuàng)建分段控制器

self.segmentedControl = [[UISegmentedControl alloc]initWithItems:arr];

self.segmentedControl.frame = CGRectMake(0, 0, 386, 30);

//? ? 加載到vv上

[vv addSubview:self.segmentedControl];

[self.segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];

//? ? 默認(rèn)第一個(gè)視圖

self.segmentedControl.selectedSegmentIndex = 0;

// 創(chuàng)建scrollView

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 94, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];

[self.view addSubview:self.scrollView];

// 設(shè)置scrollView的內(nèi)容

self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 6, [UIScreen mainScreen].bounds.size.height - 64);

self.scrollView.pagingEnabled = YES;

self.scrollView.bounces = YES;

// 創(chuàng)建控制器

self.oneVC = [OneViewController new];

self.twoVC = [TwoViewController new];

self.threeVC = [ThreeViewController new];

self.fourVC = [FourViewController new];

self.fiveVC = [FiveViewController new];

self.sixVC = [SixViewController new];

// 添加為self的子控制器

[self addChildViewController:self.oneVC];

[self addChildViewController:self.twoVC];

[self addChildViewController:self.threeVC];

[self addChildViewController:self.fourVC];

[self addChildViewController:self.fiveVC];

[self addChildViewController:self.sixVC];

//? ? 每個(gè)視圖

self.oneVC.view.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.twoVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.threeVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*2, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.fourVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*3, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.fiveVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*4, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.sixVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*5, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

//加載到滾動(dòng)視圖上

[self.scrollView addSubview:self.oneVC.view];

[self.scrollView addSubview:self.twoVC.view];

[self.scrollView addSubview:self.fourVC.view];

[self.scrollView addSubview:self.fiveVC.view];

[self.scrollView addSubview:self.threeVC.view];

[self.scrollView addSubview:self.sixVC.view];

self.scrollView.delegate = self;

//? ? 按鈕

btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//按鈕位置

btn.frame = CGRectMake(385, 0, 30, 30);

//? ? 按鈕背景顏色

btn.backgroundColor = [UIColor whiteColor];

[btn setTitle:@"+" forState:0];

btn.layer.borderWidth = 1;

btn.layer.borderColor = [UIColor blueColor].CGColor;

//? ? 按鈕點(diǎn)擊事件

[btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];

//? ? 加載

[vv addSubview:btn];

}

//分段控制器方法

- (void)segmentedControlAction:(UISegmentedControl *)sender

{

[self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.scrollView.frame.size.width, 0) animated:YES];

}

//滾動(dòng)視圖代理方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

NSInteger n = scrollView.contentOffset.x / scrollView.frame.size.width;

self.segmentedControl.selectedSegmentIndex = n;

}

//實(shí)現(xiàn)按鈕方法

-(void)btnclick{

//初始化內(nèi)容視圖控制器

PopViewController *popview = [[PopViewController alloc]init];

//設(shè)置大小

popview.preferredContentSize = CGSizeMake(150, 140);

// 設(shè)置彈出效果

popview.modalPresentationStyle = UIModalPresentationPopover;

//初始化一個(gè)popover

self.pop = popview.popoverPresentationController;

self.pop.delegate = self;

//設(shè)置彈出視圖的顏色

self.pop.backgroundColor = [UIColor whiteColor];

//設(shè)置popover的來源按鈕(以button誰為參照)

self.pop.sourceView = btn;

//設(shè)置彈出視圖的位置(以button誰為參照)

self.pop.sourceRect = btn.bounds;

//箭頭的方向 設(shè)置成UIPopoverArrowDirectionAny 會(huì)自動(dòng)轉(zhuǎn)換方向

self.pop.permittedArrowDirections = UIPopoverArrowDirectionUp;

//模態(tài)出彈框

[self presentViewController:popview animated:YES completion:nil];

}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{

return UIModalPresentationNone;

}

//點(diǎn)擊蒙版是否消失筷登,默認(rèn)為yes;

-(BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{

return YES;

}

//彈框消失時(shí)調(diào)用的方法

-(void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{

NSLog(@"彈框已經(jīng)消失");

}

小視圖popviewcontroll

@interface PopViewController ()<UITableViewDelegate,UITableViewDataSource>{

//數(shù)組

UITableView *table;

//? ? 表格元素

NSArray *imgarr,*name;

}

-(void)viewdidload

table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

//? ? 代理

table.delegate = self;

table.dataSource = self;

//? ? 加載

[self.view addSubview:table];

//? ? 數(shù)組內(nèi)容

imgarr = @[@"1",@"2",@"3"];

name = @[@"確認(rèn)添加",@"刪除添加",@"關(guān)閉"];

//分區(qū)行數(shù)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return imgarr.count;

}

//內(nèi)容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];

}

cell.imageView.image = [UIImage imageNamed:imgarr[indexPath.row]];

cell.textLabel.text = name[indexPath.row];

return cell;

}

//點(diǎn)擊行相應(yīng)事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//? ? 判斷第幾行

if (indexPath.row == 0) {

//? ? ? ? 提示框

UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"確認(rèn)添加"message:@"操作以完成" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];

[alertController addAction:action];

[self presentViewController:alertController animated:YES completion:nil];

}

//如果等于第二行

else if (indexPath.row == 1){

//? ? ? ? 提示框

UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"刪除添加"message:@"操作以完成" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];

[alertController addAction:action];

[self presentViewController:alertController animated:YES completion:nil];

}

else if (indexPath.row == 2){

//? ? ? ? 提示框

UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"關(guān)閉"message:@"操作以完成" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];

[alertController addAction:action];

[self presentViewController:alertController animated:YES completion:nil];

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌根盒,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異逊桦,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)抑进,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門强经,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人寺渗,你說我怎么就攤上這事匿情±计龋” “怎么了?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵炬称,是天一觀的道長汁果。 經(jīng)常有香客問我,道長玲躯,這世上最難降的妖魔是什么据德? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮跷车,結(jié)果婚禮上棘利,老公的妹妹穿的比我還像新娘。我一直安慰自己朽缴,他們只是感情好善玫,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著密强,像睡著了一般茅郎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上或渤,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天系冗,我揣著相機(jī)與錄音,去河邊找鬼薪鹦。 笑死毕谴,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的距芬。 我是一名探鬼主播涝开,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼框仔!你這毒婦竟也來了舀武?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤离斩,失蹤者是張志新(化名)和其女友劉穎银舱,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體跛梗,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡寻馏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了核偿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片诚欠。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出轰绵,到底是詐尸還是另有隱情粉寞,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布左腔,位于F島的核電站唧垦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏液样。R本人自食惡果不足惜振亮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望鞭莽。 院中可真熱鬧坊秸,春花似錦、人聲如沸撮抓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽丹拯。三九已至,卻和暖如春荸恕,著一層夾襖步出監(jiān)牢的瞬間乖酬,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工融求, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留咬像,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓生宛,卻偏偏與公主長得像县昂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子陷舅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容