xcode 有没有办法改变 UIToolbar 的高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2135407/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:15:20  来源:igfitidea点击:

Is there a way to change the height of a UIToolbar?

iphonexcodeinterface-builderuitoolbar

提问by mac_55

I've got an UIToolbarin Interface Builder and I've noticed that it's locked to being 44px tall. Of course I'd like to make this larger.

我有一个UIToolbarin Interface Builder,我注意到它被锁定为 44px 高。当然,我想让这个更大。

Does Apple allow resizing of this control? If so, how do I go about it?

Apple 是否允许调整此控件的大小?如果是这样,我该怎么做?

回答by David Kanarek

Sure, just set its frame differently:

当然,只是设置不同的框架:

[myToolbar setFrame:CGRectMake(0, 50, 320, 35)];

This will make your toolbar 35 pixels tall. Of course this requires an IBOutlet or creating the UIToolbar programmatically, but that's very easy to do.

这将使您的工具栏高 35 像素。当然,这需要一个 IBOutlet 或以编程方式创建 UIToolbar,但这很容易做到。

回答by ericmaciel

If that does not work in SDK 6, it is possible to solve as below:

如果这在 SDK 6 中不起作用,可以按如下方式解决:

Select the toolbar element and choose Editor > Pin > Height to create a constraint. Go to your View Controller Scene and select the created Height(44) constraint, then put the value you want.

选择工具栏元素并选择 Editor > Pin > Height 以创建约束。转到您的视图控制器场景并选择创建的 Height(44) 约束,然后输入您想要的值。

回答by christophercotton

I found that if I set the frame on the iPad, when hiding/showing the Toolbar would reset itself back to a height of 44 pixels. I ended up having to override UIToolbar and change the method:

我发现如果我在 iPad 上设置框架,当隐藏/显示工具栏时,工具栏会将自身重置为 44 像素的高度。我最终不得不覆盖 UIToolbar 并更改方法:

// return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (CGSize)sizeThatFits:(CGSize)size {
    CGSize result = [super sizeThatFits:size];
    result.height = 55;
    return result;
};     

This would correct adjust the height even with the hide/show.

即使隐藏/显示,这也可以正确调整高度。

回答by matt

In iOS 6, with autolayout, the simplest approach is a UIToolbar subclass in which you override instrinsicContentSize. Here's code from one my apps, where the toolbar is tall. Its sides and bottom are pinned to the sides and bottom of the superview as usual.

在 iOS 6 中,使用自动布局,最简单的方法是在 UIToolbar 子类中覆盖instrinsicContentSize. 这是我的一个应用程序的代码,其中工具栏很高。它的侧面和底部像往常一样固定在超级视图的侧面和底部。

-(CGSize)intrinsicContentSize {
    return CGSizeMake(UIViewNoIntrinsicMetric, 85);
}

回答by cavalleydude

For Xcode 7.1 iOS 9, in auto layout, the size is locked to 44px. The Xcode menu option Editor > Pin > Height is not there, instead do the following action:

对于 Xcode 7.1 iOS 9,在自动布局中,大小被锁定为 44px。Xcode 菜单选项 Editor > Pin > Height 不存在,而是执行以下操作:

In InterfaceBuilder, click the toolbar element to select it. Control+Drag down anywhere in the toolbar and release, a popup menu will display showing the option "Height" at the top, select it.

在 InterfaceBuilder 中,单击工具栏元素以将其选中。Control+在工具栏中的任意位置向下拖动并释放,将显示一个弹出菜单,显示顶部的“高度”选项,选择它。

You now have a Height constraint to work with and adjust as necessary.

您现在可以使用高度约束并根据需要进行调整。

回答by Radu Di??

You could also just edit the xib file:

您也可以只编辑 xib 文件:

open it as source code and find the entry that defines the frame for the UIToolbar, something along the lines of

将其作为源代码打开并找到定义 UIToolbar 框架的条目,类似于

<string key="NSFrame">{{0,420}, {320,44}}</string>

<string key="NSFrame">{{0,420}, {320,44}}</string>

and just change the value for 44 to whatever size you need.

只需将 44 的值更改为您需要的任何大小。

This way the toolbar will be taller, and in InterfaceBuilder you'll see the new size grayed out and you'll be unable to change it, but you don't need any outlets or code.

这样,工具栏会更高,并且在 InterfaceBuilder 中,您会看到新的尺寸变灰,您将无法更改它,但您不需要任何插座或代码。

回答by Alberto Lopez

As long as you have a height constraint on the toolbar you can use this little snippet that has helped me adjust heights for classes that inherit from UIView

只要您在工具栏上有高度限制,您就可以使用这个小片段,它帮助我调整从 UIView 继承的类的高度

-(void)setHeightConstraintTo:(CGFloat)height forView:(UIView *)view{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d",          NSLayoutAttributeHeight];
    NSArray *filteredArray = [view.constraints filteredArrayUsingPredicate:predicate];
    if(filteredArray.count > 0){
        NSLayoutConstraint *constraint = filteredArray.firstObject;
        constraint.constant = height;
    }
 }

回答by Robin Macharg

I'm not sure how this would sit with Apple - and of course it depends how you wish to use the toolbar - but you can add a default UIView and change its class in the property inspector to UIToolbar. This gives you transparency and customisability (in this case height) for free, at the expense of the layout of bar button items.

我不确定 Apple 会如何处理 - 当然这取决于您希望如何使用工具栏 - 但您可以添加默认 UIView 并将其在属性检查器中的类更改为 UIToolbar。这以牺牲条形按钮项目的布局为代价,免费为您提供透明度和可定制性(在这种情况下为高度)。

回答by Ragul Parani

Swift Solution:

迅捷解决方案:

myToolbar.frame = CGRect(x: myToolbar.frame.origin.x, y: myToolbar.frame.origin.y, width: myToolbar.frame.size.width, height: 20)

The CGRectMakeis obsolete. This can be replaced with the CGRect. This will set the height of the toolbar to 20. The same works for Segmented control as well.

CGRectMake是过时的。这可以替换为CGRect. 这会将工具栏的高度设置为 20。这同样适用于 Segmented 控件。

回答by FredericK

In interface builder, there is also the possibility to use "User Defined Runtime Attributes".

在界面构建器中,还可以使用“用户定义的运行时属性”。

Simply add an entry with keypath set to "frame" of type "Rect" and set the value you want.

只需添加一个键路径设置为“矩形”类型的“框架”的条目并设置您想要的值。

enter image description here

在此处输入图片说明