`

【iPhone代码片段之八】如何自定义Tab Bar Button .

ios 
阅读更多
核心代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    CGRect frame = [[UIScreen mainScreen] bounds];
    //NSLog(@"x=%.2f,y=%.2f,width=%.2f,height=%.2f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
    self.window = [[UIWindow alloc] initWithFrame:frame];
    [[NSBundle mainBundle]loadNibNamed:@"TabBarController" owner:self options:nil];
    //[self.window addSubview:tabBarController.view];
    self.window.rootViewController = self.tabBarController;
    //添加中间的TabBarButton
    [self addCenternTabBarButton];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
}


-(void)addCenternTabBarButton{
    //NSLog(@"addCenternTabBarButton");
    //创建一个自定义button
    UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //初始化button的背景图片                           
    UIImage *centerButtonImg = [UIImage imageNamed:@"centerTabBarItem.png"];
    //设置button的frame
    centerButton.frame = CGRectMake(0, 0, centerButtonImg.size.width, centerButtonImg.size.height);
    //设置button的背景图片
    [centerButton setBackgroundImage:centerButtonImg forState:UIControlStateNormal];
    //设置button的action
    [centerButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat imageHeight = centerButtonImg.size.height;
    CGFloat barHeight = self.tabBarController.tabBar.frame.size.height;
    //NSLog(@"imageHeight=%2.f,barHeight=%.2f",imageHeight,barHeight);
    CGFloat delta = imageHeight-barHeight;
    //NSLog(@"delta=%.2f",delta);
    
    //设置centerButton的中心位置
    if(delta<=0){//如果图片高度小于等于TabBar高度
        centerButton.center = self.tabBarController.tabBar.center;
    }else{//如果图片高度大于TabBar高度
        CGPoint center = self.tabBarController.tabBar.center;
        center.y = center.y - delta/2.0;
        centerButton.center = center;
    }
    //将centerButton加入到tabBarController中
    [self.tabBarController.view addSubview:centerButton];
}


效果图如下:
[img]

[/img]
  • 大小: 90.3 KB
分享到:
评论

相关推荐

    AP 手机-iPhone库

    适合IPhone的快速原型开发,里面有组件如下: iPhone Segemented Tab Control Table View 1 - Labels Label Table View 2 - Switches and Labels Slider - Border Group Table View Table View 3 - Selected Option ...

    iPhone开发【十】多视图技术总结之二:Navigation

    9. **Tab Bar和Navigation的结合**:在许多应用中,Navigation Controller常常与Tab Bar Controller结合使用,创建一个多层级、多选项卡的用户界面。 通过博客文章中的源代码分析,我们可以学习到如何实际操作这些...

    Axure中使用的iPhone UI库

    2. **导航元素**:包括底部导航栏(Tab Bar)、顶部导航栏(NavigationBar)、抽屉式侧边栏(Sidebar)以及各种滑动切换组件,这些都是iOS应用中常见的导航模式。 3. **控件**:库中会有各种iOS标准控件,如按钮...

    史上最全的ios开发源码

    选项卡之Arc Tab 选项卡之LSTabs 选择器类 选择器类--PickerView with Search Bar 选择器类--Value Selector 选择器类--Wheel Menu 选择器类--老虎机 选择器之定制多选的PickerView 音频声效类 音频声效 ...

    android开发资料大全

    Android 小項目之---Iphone拖动图片特效 (附源码) 一个完整的新浪微博客户端android版OAuth认证示例 超爽的android抽屉效果 65个Android实例教程汇总 基本控件及基本动画效果dem 2011android面试题目及其答案大全...

    京东首页静态模板

    &lt;div data-type="bar" class="J-trigger jdm-toolbar-tab jdm-tbar-tab-cart"&gt; &lt;i class="tab-ico"&gt;&lt;/i&gt; &lt;em class="tab-text"&gt;购物车 &lt;/em&gt; &lt;/div&gt; &lt;div data-type="bar" clstag="h|keycount|cebianlan_h_...

Global site tag (gtag.js) - Google Analytics