移动开发技术前沿

SegmentControl的基本设置

2015-07-28  本文已影响13487人  Little_Dragon
if (self == [LXLArenaNavController class]) {


    // 设置导航条背景图片
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];

    UIImage *image =  [UIImage imageNamed:@"NLArenaNavBar64"];
    // 图片拉伸 ,当然可以直接在图片上进行设置
    image =[image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
    // 设置导航条背景图片  ,样式必须为UIBarMetriDefault
    // 只有UIBarMetriDefault的样式,导航控制器的子控制器view的尺寸才不包括导航条的那部分
    [bar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }

// 自定义控制器的view
- (void)loadView
{
    UIImageView *bgV = [[UIImageView alloc] initWithFrame:XMGKeyWindow.bounds];

    bgV.image = [UIImage imageNamed:@"NLArenaBackground"];

    // 一定要运行用户交互
    bgV.userInteractionEnabled = YES;

    self.view = bgV;
}

    // 设置导航条的titleView内容

    UISegmentedControl *segContontrol = [[UISegmentedControl alloc] initWithItems:@[@"足球",@"篮球"]];

    // 设置UISegmentedControl选中的图片
    [segContontrol setBackgroundImage:[UIImage imageNamed:@"CPArenaSegmentSelectedBG"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];


    // 正常的图片
    [segContontrol setBackgroundImage:[UIImage imageNamed:@"CPArenaSegmentBG"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    [segContontrol setTintColor:[UIColor colorWithRed:0 green:142/255.0 blue:143/255.0 alpha:1]];

    // 设置选中的文字颜色
    [segContontrol setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateSelected];

    segContontrol.selectedSegmentIndex = 0;


    self.navigationItem.titleView = segContontrol;

上一篇 下一篇

猜你喜欢

热点阅读