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

发布于 2024-08-18 17:57:28 字数 126 浏览 9 评论 0原文

我在 Interface Builder 中有一个 UIToolbar,我注意到它的高度被锁定为 44px。当然,我想把它做得更大。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

嘿嘿嘿 2024-08-25 17:57:28

当然,只需以不同的方式设置其框架:

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

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

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.

短叹 2024-08-25 17:57:28

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

选择工具栏元素,然后选择“编辑器”>“编辑器”。销>创建约束的高度。
转到视图控制器场景并选择创建的高度(44)约束,然后输入所需的值。

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

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.

冰葑 2024-08-25 17:57:28

我发现如果我在 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;
};     

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

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:

// 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.

余罪 2024-08-25 17:57:28

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

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

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.

-(CGSize)intrinsicContentSize {
    return CGSizeMake(UIViewNoIntrinsicMetric, 85);
}
淡笑忘祈一世凡恋 2024-08-25 17:57:28

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

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

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

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:

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.

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

人间不值得 2024-08-25 17:57:28

您也可以只编辑 xib 文件:

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

{{0,420}, { 320,44}}

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

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

You could also just edit the xib file:

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

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

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

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.

迷离° 2024-08-25 17:57:28

只要工具栏上有高度限制,您就可以使用这个小片段,它帮助我调整从 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;
    }
 }

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

-(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;
    }
 }
时光倒影 2024-08-25 17:57:28

我不确定 Apple 对此有何看法 - 当然,这取决于您希望如何使用工具栏 - 但您可以添加默认的 UIView 并将其在属性检查器中的类更改为 UIToolbar。这免费为您提供透明度和可定制性(在本例中为高度),但代价是栏按钮项目的布局。

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.

迷爱 2024-08-25 17:57:28

Swift 解决方案:

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

CGRectMake 已过时。这可以用CGRect替换。这会将工具栏的高度设置为 20。这对于分段控件也适用。

Swift Solution:

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

The CGRectMake is 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.

月光色 2024-08-25 17:57:28

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

只需添加一个将关键路径设置为“矩形”类型的“框架”的条目,然后设置所需的值即可。

输入图片此处描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文