美文网首页
ios -- 添加分段控件到导航条并居中

ios -- 添加分段控件到导航条并居中

作者: 井底蛙之呱呱 | 来源:发表于2017-11-14 10:08 被阅读20次
       NSArray *arr = @[@"消息",@"通知"];
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:arr];
        segmentedControl.frame = CGRectMake(0, 0, 100, 30);
        
        segmentedControl.selectedSegmentIndex = 0;
        
        segmentedControl.tintColor = [UIColor blueColor];
        
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        
        [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:)
         
                   forControlEvents:UIControlEventValueChanged];
    //    self.navigationController.navigationItem.titleView = segmentedControl;
        [self.navigationItem setTitleView:segmentedControl];
        
    
    }
    
    - (void)indexDidChangeForSegmentedControl:(id)sender {
        if ([sender selectedSegmentIndex] == 0) {
            self.view.backgroundColor = [UIColor redColor];
        } else if ([sender selectedSegmentIndex] == 1) {
            self.view.backgroundColor = [UIColor blueColor];
        }
    }
    

    相关文章

      网友评论

          本文标题:ios -- 添加分段控件到导航条并居中

          本文链接:https://www.haomeiwen.com/subject/mssxvxtx.html