ios 我们如何创建更大的中心 UITabBar Item
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30527738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do we create a bigger center UITabBar Item
提问by JayVDiyk
I am wondering how do we create a bigger center UITabBar like the shot below? Its really beautiful!!!!
我想知道我们如何创建一个更大的中心 UITabBar,如下图所示?真的很漂亮!!!!
采纳答案by Manuel Escrig
I recommend you taking a look at the following article. It explains how to customise a tab bar raising the main button.
我建议你看看下面的文章。它解释了如何自定义提升主按钮的标签栏。
Code:
代码:
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.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];
Guide: https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
指南:https: //github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
回答by ZameerMoh
2017 Answer - With Just Xcode
2017 答案 - 仅使用 Xcode
Click the tab bar button within the view controller of the particular tab bar item you want to make prominent,
单击要突出显示的特定标签栏项目的视图控制器中的标签栏按钮,
Remove the text, just set the image inset top to -25 of the tab bar button.
删除文本,只需将图像插入顶部设置为标签栏按钮的 -25。
Like Below image
喜欢下图
After that
在那之后
goto assets,
select the image you set in tab bar button,
set the property Rendering As to Original Image(in case if you have a colourful button or else it would render as one colour)
Like below,
转到资产,
选择您在标签栏按钮中
设置的图像,将属性渲染为原始图像(如果您有彩色按钮,否则它将呈现为一种颜色)
如下所示,
Now, You will get it like you wanted,
EDIT: To make upper half clickable, inherit UITabBar
编辑:要使上半部分可点击,请继承 UITabBar
class ProminentTabBar: UITabBar {
var prominentButtonCallback: (()->())?
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard let items = items, items.count>0 else {
return super.hitTest(point, with: event)
}
let middleItem = items[items.count/2]
let middleExtra = middleItem.imageInsets.top
let middleWidth = bounds.width/CGFloat(items.count)
let middleRect = CGRect(x: (bounds.width-middleWidth)/2, y: middleExtra, width: middleWidth, height: abs(middleExtra))
if middleRect.contains(point) {
prominentButtonCallback?()
return nil
}
return super.hitTest(point, with: event)
}
}
And in TabBarController add this
在 TabBarController 添加这个
override func viewDidLoad() {
super.viewDidLoad()
let prominentTabBar = self.tabBar as! ProminentTabBar
prominentTabBar.prominentButtonCallback = prominentTabTaped
}
func prominentTabTaped() {
selectedIndex = (tabBar.items?.count ?? 0)/2
}
And remmber there is no nice solution when it comes to UITabBar :-)
并且记住,当涉及到 UITabBar 时,没有好的解决方案 :-)
回答by juanjo
Swift 3, 4:
斯威夫特 3、4:
I use this code in the viewDidLoad
of my subclass of UITabBarController
:
我在viewDidLoad
我的子类中使用此代码UITabBarController
:
let button = UIButton()
button.setImage(UIImage(named: "home"), for: .normal)
button.sizeToFit()
button.translatesAutoresizingMaskIntoConstraints = false
tabBar.addSubview(button)
tabBar.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
tabBar.topAnchor.constraint(equalTo: button.centerYAnchor).isActive = true
Sometimes I also set button.adjustsImageWhenHighlighted = false
to mimic the behavior of the other items, or change the constraint constant
property to move the button up or down.
有时我也设置button.adjustsImageWhenHighlighted = false
模仿其他项目的行为,或更改约束constant
属性以向上或向下移动按钮。
回答by Michael Dautermann
Here's the ported Swift 3 version of @Kakashi's answer (and +1 to them), which I put in my custom UITabBarController subclass:
这是@Kakashi 答案的移植 Swift 3 版本(并对他们 +1),我将其放入我的自定义 UITabBarController 子类中:
override func viewDidLoad() {
if let newButtonImage = UIImage(named: "new__button") {
self.addCenterButton(withImage: newButtonImage, highlightImage: newButtonImage)
}
}
func handleTouchTabbarCenter(sender : UIButton)
{
if let count = self.tabBar.items?.count
{
let i = floor(Double(count / 2))
self.selectedViewController = self.viewControllers?[Int(i)]
}
}
func addCenterButton(withImage buttonImage : UIImage, highlightImage: UIImage) {
let paddingBottom : CGFloat = 10.0
let button = UIButton(type: .custom)
button.autoresizingMask = [.flexibleRightMargin, .flexibleTopMargin, .flexibleLeftMargin, .flexibleBottomMargin]
button.frame = CGRect(x: 0.0, y: 0.0, width: buttonImage.size.width / 2.0, height: buttonImage.size.height / 2.0)
button.setBackgroundImage(buttonImage, for: .normal)
button.setBackgroundImage(highlightImage, for: .highlighted)
let rectBoundTabbar = self.tabBar.bounds
let xx = rectBoundTabbar.midX
let yy = rectBoundTabbar.midY - paddingBottom
button.center = CGPoint(x: xx, y: yy)
self.tabBar.addSubview(button)
self.tabBar.bringSubview(toFront: button)
button.addTarget(self, action: #selector(handleTouchTabbarCenter), for: .touchUpInside)
if let count = self.tabBar.items?.count
{
let i = floor(Double(count / 2))
let item = self.tabBar.items![Int(i)]
item.title = ""
}
}
回答by Kakashi
For hide UITabbar, I make custom UITabbarController and insert this method.
为了隐藏 UITabbar,我制作了自定义 UITabbarController 并插入了这个方法。
- (void)viewDidLoad
{
[super viewDidLoad];
[self addCenterButtonWithImage:[UIImage imageNamed:@"logo"] highlightImage:[UIImage imageNamed:@"logo"]];
}
- (void)addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
float paddingBottom = 10;
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGRect rectBoundTabbar = [self.tabBar bounds];
float xx = CGRectGetMidX(rectBoundTabbar);
float yy = CGRectGetMidY(rectBoundTabbar) - paddingBottom;
button.center = CGPointMake(xx, yy);
[self.tabBar addSubview:button];
[self.tabBar bringSubviewToFront:button];
// add handle
[button addTarget:self action:@selector(handleTouchTabbarCenter:) forControlEvents:UIControlEventTouchUpInside];
// hide title item menu
NSInteger count = [self.tabBar.items count];
NSInteger i = floor(count / 2.0);
UITabBarItem *item = [self.tabBar.items objectAtIndex:i];
[item setTitle:nil];
}
- (void)handleTouchTabbarCenter:(id)sender
{
// go to some view
}
回答by mpatzer
I've taken Manuel's example (the accepted answer) and added an adjustment for the bottom safe area insets due to issues with iPhone X.
由于 iPhone X 的问题,我以 Manuel 的示例(已接受的答案)为例,并添加了对底部安全区域插图的调整。
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.tabBar.frame.size.height;
CGPoint center = self.tabBar.center;
if (heightDifference >= 0) {
center.y = center.y - heightDifference/2.0;
}
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
center.y = center.y - bottomPadding;
}
[self.view addSubview:button];
回答by Hardik Thakkar
For Swift
对于斯威夫特
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(2.0)), dispatch_get_main_queue(), {
let button: UIButton = UIButton(type: .Custom)
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
button.frame = CGRectMake(0.0, win.frame.size.height - 65, 55, 55)
button.center = CGPoint(x:win.center.x , y: button.center.y)
button.setBackgroundImage(UIImage(named: "Camera") , forState: .Normal)
button.setBackgroundImage(UIImage(named: "Camera"), forState: .Highlighted)
win.addSubview(button)
});
回答by Joan Cardona
I followed @Michael Dautermann answer but the button never registers the tap so I modified it to make it work:
我遵循了@Michael Dautermann 的回答,但该按钮从未注册过水龙头,因此我对其进行了修改以使其正常工作:
func handleTouchTabbarCenter()
{
if let count = self.tabBar.items?.count
{
let i = floor(Double(count / 2))
self.selectedViewController = self.viewControllers?[Int(i)]
}
}
func addCenterButton(withImage buttonImage : UIImage, highlightImage: UIImage) {
self.centerButton = UIButton(type: .custom)
self.centerButton?.autoresizingMask = [.flexibleRightMargin, .flexibleTopMargin, .flexibleLeftMargin, .flexibleBottomMargin]
self.centerButton?.frame = CGRect(x: 0.0, y: 0.0, width: buttonImage.size.width, height: buttonImage.size.height)
self.centerButton?.setBackgroundImage(buttonImage, for: .normal)
self.centerButton?.setBackgroundImage(highlightImage, for: .highlighted)
self.centerButton?.isUserInteractionEnabled = true
let heightdif: CGFloat = buttonImage.size.height - (self.tabBar.frame.size.height);
if (heightdif < 0){
self.centerButton?.center = (self.tabBar.center)
}
else{
var center: CGPoint = (self.tabBar.center)
center.y = center.y - 24
self.centerButton?.center = center
}
self.view.addSubview(self.centerButton!)
self.tabBar.bringSubview(toFront: self.centerButton!)
self.centerButton?.addTarget(self, action: #selector(handleTouchTabbarCenter), for: .touchUpInside)
if let count = self.tabBar.items?.count
{
let i = floor(Double(count / 2))
let item = self.tabBar.items![Int(i)]
item.title = ""
}
}