Question About UIButton And UITabBar
I am trying to place a UIButton with an image above my UITabBar.
I added this code in my applicationDidFinishLaunching method, but I'm getting some errors with tabBar
being unrecognized.
I've built the TabBarController and NavController all in IB.
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"addTabButton.png"];
UIImage *highlightImage = [UIImage imageNamed:@"addTabButtonHighlight.png"];
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.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
Edit:
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"addTabButton.png"];
UIImage *highlightImage = [UIImage imageNamed:@"addTabButtonHighlight.png"];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:high开发者_JAVA技巧lightImage forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(btnTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button];
Can you post your code of the outlets and properties in the .h file?
Is it possible that you made them in your main ViewController? Because applicationDidFinishLaunching
is a appDelegate method and you normally shouldn't do UI things there, just in the viewControllers.
applicationDidFinishLaunching is not the appropriate place to put such a method. Use viewDidLoad or viewWillAppear instead.
It seems you are following this blog post http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/ to create custom TabBar but you are not following it properly. What you are doing is adding button as subview to normal viewController's View while you need to add the button to TabBar's View. Try to add button when your tabBarContoller is going to display. Like if you are removing navigation Controller's View from mainWindow and adding tabBarController's view there then before adding tabBarController's view to window add your button to tabBarController's tabBar.view so your code will look like this
[tabBarController.tabBar.view addSubview:yourButton];
[window addSubView:tabBarController.view];
Try and let me know what you got.....
精彩评论