1

是否可以使用界面生成器在标签栏控制器上添加按钮?

我试图在界面构建器中执行此操作,但每次我在选项卡栏上添加按钮时,它都会用按钮填满屏幕的其余部分,而不是仅仅将按钮放在选项卡栏上。

类似于 Instagram 中的相机。

4

2 回答 2

0

这就是我解决问题的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImage *buttonImage = [UIImage imageNamed:@"cameraTabBarItem.png"];
    UIImage *highlightImage = [UIImage imageNamed:@"cameraTabBarItem.png"];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    CGFloat heightDifference = buttonImage.size.height - self.tabBarController.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBarController.tabBar.center;
    else
    {
        CGPoint center = self.tabBarController.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        button.center = center;
    }
    [button addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarController.view addSubview:button];
}
于 2012-11-01T00:46:16.477 回答
0

您可能需要将按钮添加为 App Delegate 窗口的子视图:

[self.window addSubview:myButton];
于 2012-11-01T00:54:48.377 回答