在tabbarcontroller上添加按钮

时间:2012-10-31 19:39:41

标签: ios xcode interface-builder uitabbarcontroller uitabbar

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

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

像instagram中的相机一样。

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

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

[self.window addSubview:myButton];