• 简单实现TabBar的自定义


    StackoverFlow上看到的,通过继承UITabBarController创建自定义TabBarController。在原有TabBar的基础上添加一个背景层,在其基础上增加三个自定义按钮,通过设置按钮的背景图片及大小即可简单实现TabBar的自定义。

    // CustomTabBarController.h

    #import <UIKit/UIKit.h>

    @interface CustomTabBarController : UITabBarController {

    UIButton *settingsButton;

    UIButton *infoButton;

    UIButton *aboutUsButton;

    }

    @property (nonatomic, retainUIButton *settingsButton;

    @property (nonatomic, retainUIButton *infoButton;

    @property (nonatomic, retainUIButton *aboutUsButton;

    -(void) addCustomElements;

    -(void) selectTab:(int)tabID;

    @end

    // CustomTabBarController.m

    #import “CustomTabBarController.h”

    @implementation CustomTabBarController

    @synthesize settingsButton, infoButton, aboutUsButton;

    - (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    }

    -(void)viewDidLoad

    {

    [super viewDidLoad];

    [self addCustomElements];

    }

    -(void)addCustomElements

    {

    // Background

    UIImageView* bgView = [[[UIImageView alloc] initWithImage:[UIImageimageNamed:@”tabBarBackground.png”]] autorelease];

    bgView.frame = CGRectMake(0, 420, 320, 60);

    [self.view addSubview:bgView];

    // Initialise our two images

    UIImage *btnImage = [UIImage imageNamed:@”settings.png”];

    UIImage *btnImageSelected = [UIImage imageNamed:@”settingsSelected.png”];

      

    self.settingsButton = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button

    settingsButton.frame CGRectMake(1042610054); // Set the frame (size and position) of the button)

    [settingsButton setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button

    [settingsButton setBackgroundImage:btnImageSelectedforState:UIControlStateHighlighted]; // Set the image for the selected state of the button

    [settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];// Set the image for the selected state of the button

    [settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateDisabled];

    [settingsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

    [settingsButton setTag:101]; // Assign the button a “tag” so when our “click” event is called we know which button was pressed.

    [settingsButton setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially

      

    // Now we repeat the process for the other buttons

    btnImage = [UIImage imageNamed:@”info.png”];

    btnImageSelected = [UIImage imageNamed:@”infoSelected.png”];

    self.infoButton = [UIButton buttonWithType:UIButtonTypeCustom];

    infoButton.frame = CGRectMake(11042610054);

    [infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];

    [infoButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];

    [infoButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];

    [infoButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

      

    [infoButton setTag:102];

      

    btnImage = [UIImage imageNamed:@”aboutUs.png”];

    btnImageSelected = [UIImage imageNamed:@”aboutUsSelected.png”];

    self.aboutUsButton = [UIButton buttonWithType:UIButtonTypeCustom];

    aboutUsButton.frame = CGRectMake(21042610054);

    [aboutUsButton setBackgroundImage:btnImage forState:UIControlStateNormal];

    [aboutUsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];

    [aboutUsButton setBackgroundImage:btnImageSelectedforState:UIControlStateHighlighted];

    [aboutUsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

      

    [aboutUsButton setTag:103];

      

    // Add my new buttons to the view

    [self.view addSubview:settingsButton];

    [self.view addSubview:infoButton];

    [self.view addSubview:aboutUsButton];

      

    // Setup event handlers so that the buttonClicked method will respond to the touch up inside event.

    [settingsButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

    [infoButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

    [aboutUsButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

    }

    - (void)buttonClicked:(id)sender

    {

    int tagNum = [sender tag];

    [self selectTab:tagNum];

    }

    - (void)selectTab:(int)tabID

    {

    switch(tabID)

    {

    case 101:

    [settingsButton setSelected:true];

    [infoButton setSelected:false];

    [aboutUsButton setSelected:false];

    break;

    case 102:

    [settingsButton setSelected:false];

    [infoButton setSelected:true];

    [aboutUsButton setSelected:false];

    break;

    case 103:

    [settingsButton setSelected:false];

    [infoButton setSelected:false];

    [aboutUsButton setSelected:true];

    break;

    }

    self.selectedIndex = tabID;

    }

    - (void)dealloc {

    [settingsButton release];

    [infoButton release];

    [aboutUsButton release];

    [super dealloc];

    }

    @end

     

     

    转载自: http://blog.163.com/l1_jun/blog/static/143863882012101545146269/

     
  • 相关阅读:
    执行游戏时出现0xc000007b错误的解决方法
    html中#include file的使用方法
    使用ServletFileUpload实现上传
    java环境变量配置
    微软之左旋转字符串
    Ubuntu下很给力的下载工具
    hibernate官方新手教程 (转载)
    mssql数据库游标批量改动符合条件的记录
    PageRank算法
    Myeclipse7.5 下载 安装 注冊 注冊码 100%成功
  • 原文地址:https://www.cnblogs.com/Cheetah-yang/p/4642971.html
Copyright © 2020-2023  润新知