我正在使用自定义标签栏和按钮。我已经在顶部位置制作了自定义标签栏。我已成功制作它。但我希望标签栏位置位于两个视图控制器的中心>我该怎么做。可能吗?? 这是我的代码
这是我的自定义标签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.
}
请帮助。谢谢!
答案 0 :(得分:-2)