Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我们如何创建一个更大的中心UITabBar项目_Ios_Uibutton_Uitabbarcontroller_Custom Controls_Uitabbar - Fatal编程技术网

Ios 我们如何创建一个更大的中心UITabBar项目

Ios 我们如何创建一个更大的中心UITabBar项目,ios,uibutton,uitabbarcontroller,custom-controls,uitabbar,Ios,Uibutton,Uitabbarcontroller,Custom Controls,Uitabbar,我想知道我们如何创建一个更大的中心,如下图所示?真是太美了 我建议您看看下面的文章。 它解释了如何自定义提升主按钮的选项卡栏 代码: UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); [button setBackgroundImage:butto

我想知道我们如何创建一个更大的中心,如下图所示?真是太美了


我建议您看看下面的文章。 它解释了如何自定义提升主按钮的选项卡栏

代码:

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];
UIButton*button=[UIButton button类型:UIButtonTypeCustom];
button.frame=CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
[按钮设置背景图像:状态的按钮图像:uicontrol状态正常];
[按钮setBackgroundImage:highlightImage for状态:uicontrol状态高亮显示];
CGFloat heightDifference=buttonImage.size.height-self.tabBar.frame.size.height;
如果(高差<0)
button.center=self.tabBar.center;
其他的
{
CGPoint center=self.tabBar.center;
center.y=center.y-高度差/2.0;
button.center=中心;
}
[self.view addSubview:按钮];
指南
对于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)
        });

对于隐藏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
}

这里是@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 = ""
        }
    }

我遵循了@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 = ""
    }
}
func handleTouchTabbarCenter()
{
如果let count=self.tabBar.items?.count
{
设i=地板(双倍(计数/2))
self.selectedViewController=self.ViewController?[Int(i)]
}
}
func添加中心按钮(带图像按钮图像:UIImage,高亮图像:UIImage){
self.centerButton=ui按钮(类型:。自定义)
self.centerButton?.autoresizingMask=[.flexibleRightMargin、.flexibleTopMargin、.flexibleLeftMargin、.flexibleBottomMargin]
self.centerButton?.frame=CGRect(x:0.0,y:0.0,宽度:buttonImage.size.width,高度:buttonImage.size.height)
self.centerButton?.setBackgroundImage(按钮图像,用于:。正常)
self.centerButton?.setBackgroundImage(高亮显示图像,用于:。高亮显示)
self.centerButton?.isUserInteractionEnabled=true
let heightdif:CGFloat=buttonImage.size.height-(self.tabBar.frame.size.height);
如果(高度差<0){
self.centerButton?.center=(self.tabBar.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:#选择器(handleTouchTabbarCenter),用于:.touchUpInside)
如果let count=self.tabBar.items?.count
{
设i=地板(双倍(计数/2))
让item=self.tabBar.items![Int(i)]
item.title=“”
}
}
Swift 3、4: 我在我的
UITabBarController
子类的
viewDidLoad
中使用此代码:

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

有时,我还设置了
button.adjustsImageWhenHighlighted=false
来模拟其他项目的行为,或者更改constraint
constant
属性来上下移动按钮。

我举了Manuel的例子(公认的答案)并且由于iPhoneX的问题,增加了底部安全区域插图的调整

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];
仅使用Xcode 单击要突出显示的特定选项卡栏项目的视图控制器中的选项卡栏按钮

删除文本,只需将选项卡栏按钮的图像插入顶部设置为-25

如下图所示

之后

转到资源,
选择您在选项卡栏按钮中设置的图像,
将属性渲染设置为原始图像(如果您有彩色按钮,或者它将渲染为一种颜色)
如下图所示,

现在,你会得到你想要的,

编辑:要使上半部分可单击,请继承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)
    }
}
在TabBarController中添加这个

override func viewDidLoad() {
    super.viewDidLoad()
    
    let prominentTabBar = self.tabBar as! ProminentTabBar
    prominentTabBar.prominentButtonCallback = prominentTabTaped
}

func prominentTabTaped() {
    selectedIndex = (tabBar.items?.count ?? 0)/2
}


请记住,对于uitabar:-)

该链接的域已过期,没有好的解决方案。在您的代码中,“self”是
uitabarcontroller
?@cohenadair
self
将是
UIViewController
。我添加了一个与Github repository的链接,其中有一个按钮点击功能handleTouchTabbarCenter未调用。yep。一样。任何新的解决方案??问题:设备旋转,按钮位置不跟随。此外,还有许多其他实例,其中按钮被重新绘制,而旧按钮仍然存在。例如,在call status barHi中,谢谢,我确实看到了我的按钮,但是handleTouchTabbarCenter没有被调用,我们可能缺少什么(我看到有更多的人有相同的错误)为什么要将按钮添加到应用程序的窗口中?将它添加到视图控制器的视图中,或者更好的是,添加到选项卡栏本身,这不是更有意义吗?我还有一些其他要求,我必须像上面的代码那样添加。这是针对我的项目需求测试的代码。对,它可以正常工作,只是将按钮与它所属的视图相关联似乎更有意义,而不是整个应用程序的窗口。谁投了否决票。。请给出相同的原因。问题是定制UITabbar时没有在窗口中添加按钮。您也需要从底部添加25个边距,否则图像将显示为椭圆形。工作正常,但单击按钮时有一个问题整个按钮颜色变为蓝色。图像没有显示如何处理它?您是否对该按钮进行了子类化?这在视觉上非常有效,但它只接受选项卡栏范围内的点击。有没有办法从圆圈顶部1/3处记录抽头?嗯。。我可以在iPhone设备上使用它,但一旦它在IPad上打开,它就离我们太远了。谢谢你,公认的答案是从2010年开始的,所以很高兴看到更新版本的更新版本:)不知道标签栏是如何工作的吗?我现在需要一堆额外的代码来让按钮打开我想要的标签?