-1

ボタン付きのカスタムタブバーを使用しています。カスタムタブバーを一番上の位置に作成しました。正常に作成できました。しかし、タブバーの位置を2つのView Controllerの中央に配置したい>どうすればよいですか?可能??これが私のコードです

これは私のカスタムタブbar.mファイルです

-(void)addCustomElements
 {
   btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn1.frame = CGRectMake(0, 264, 65, 40); // Set the frame (size and position) of the button)
[btn1 setBackgroundColor:[UIColor darkGrayColor]];
[btn1.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:16.0]];
[btn1 setTitle:@"Info" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn1 setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn1 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

btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn2.frame = CGRectMake(64, 264, 65, 40); // Set the frame (size and position) of the button)
[btn2 setBackgroundColor:[UIColor darkGrayColor]];
[btn2.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:16.0]];
[btn2 setTitle:@"Book" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn2 setTag:1]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn2 setSelected:true];


btn3 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn3.frame = CGRectMake(128, 264, 65, 40); // Set the frame (size and position) of the button)
[btn3 setBackgroundColor:[UIColor darkGrayColor]];
[btn3.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:16.0]];
[btn3 setTitle:@"Menu" forState:UIControlStateNormal];
[btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn3 setTag:2]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn3 setSelected:true];

btn4 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn4.frame = CGRectMake(193, 264, 65, 40); // Set the frame (size and position) of the button)
[btn4.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:16.0]];
[btn4 setBackgroundColor:[UIColor darkGrayColor]];
[btn4 setTitle:@"Reviews" forState:UIControlStateNormal];
[btn4 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn4 setTag:3]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn4 setSelected:true];

btn5 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn5.frame = CGRectMake(258, 264, 65, 40); // Set the frame (size and position) of the button)
[btn5.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:16.0]];
[btn5 setBackgroundColor:[UIColor darkGrayColor]];
[btn5 setTitle:@"Map" forState:UIControlStateNormal];
[btn5 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn5 setTag:4]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn5 setSelected:true];


// Add my new buttons to the view
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
[self.view addSubview:btn4];
 [self.view addSubview:btn5];

// Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
[btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn5 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 0:
        [btn1 setSelected:true];
        [btn2 setSelected:false];
        [btn3 setSelected:false];
        [btn4 setSelected:false];
        [btn5 setSelected:false];
        break;
    case 1:
        [btn1 setSelected:false];
        [btn2 setSelected:true];
        [btn3 setSelected:false];
        [btn4 setSelected:false];
        [btn5 setSelected:false];
        break;
    case 2:
        [btn1 setSelected:false];
        [btn2 setSelected:false];
        [btn3 setSelected:true];
        [btn4 setSelected:false];
        [btn5 setSelected:false];
        break;
    case 3:
        [btn1 setSelected:false];
        [btn2 setSelected:false];
        [btn3 setSelected:false];
        [btn4 setSelected:true];
        [btn5 setSelected:false];
        break;
    case 4:
        [btn1 setSelected:false];
        [btn2 setSelected:false];
        [btn3 setSelected:false];
        [btn4 setSelected:false];
        [btn5 setSelected:true];
        break;
}

    if (self.selectedIndex == tabID) {
    UINavigationController *navController = (UINavigationController *)[self selectedViewController];
    [navController popToRootViewControllerAnimated:YES];
} else {
    self.selectedIndex = tabID;
}


 }
- (void)viewDidLoad
{
[super viewDidLoad];

[self hideExistingTabBar];
[self addCustomElements];

// Do any additional setup after loading the view.

}

助けてください。よろしくお願いします!!

4

1 に答える 1

-2

この方法で試すことができます。ここにリンクとソースコードがあります。これで、タブの位置を変更できます

https://github.com/hollance/MHTabBarController

願っています、これはあなたを助けます。

于 2013-10-16T07:33:52.397 に答える