简体   繁体   English

将UISegmentedControl放置在按钮栏中

[英]Positioning a UISegmentedControl in a button bar

I'm trying to use a UISegmentedControl as a custom right button bar item. 我正在尝试将UISegmentedControl用作自定义右键按钮栏项目。 Using Interface Builder, when I drag the control into the navigation bar, it gets sized to be quite wide (too wide, considering the segment contents): 使用Interface Builder,当我将控件拖到导航栏中时,它的大小将变得很宽(考虑到段内容,太宽了):

分段控件已添加到导航栏

I've tried setting the widths of the segments to something that I think looks good and the overall control resizes, but it is pinned to the left side of the original space it was allocated. 我尝试将段的宽度设置为我认为看起来不错并且可以调整整体控件的大小,但是它固定在分配的原始空间的左侧。 I can't seem to find a way of moving it from there. 我似乎找不到从那里移动它的方法。 Here's a picture of what this looks like in IB in landscape: 这是IB景观中的外观图:

Interface Builder的屏幕截图

and here's what it looks like running on an iPhone 7 Plus simulator in portrait mode: 这是在iPhone 7 Plus模拟器上以纵向模式运行时的样子:

在iPhone 7 Plus模拟器上运行

As you can see, the segmented control is positioned way too far toward the center. 如您所见,分段控件的位置离中心太远。

I tried a hack (suggested in this thread ) of embedding the segmented control inside another view, leaving the containing view to be full width of the bar button item and constraining the segmented view to be on the right of it's container. 我尝试了一种破解方法(建议在此线程中进行 ),将分段控件嵌入到另一个视图中,使包含的视图保持为bar按钮项目的全宽,并将分段视图限制在其容器的右侧。 That gets it over to the right, but here's the effect of running that on an iPhone 7 simulator (somewhat narrower than the 7 Plus): 这样可以正确解决问题,但这是在iPhone 7模拟器(比7 Plus窄一些)上运行该模拟器的效果:

在此处输入图片说明

Here the problem is that the title, which is supposed to be centered, has been pushed off to the left. 这里的问题是应该居中的标题已被推到左侧。 I assume that this is because the (transparent) bar button item is taking up so much more width than what is needed just for the segmented control. 我认为这是因为(透明)条形按钮项占用的宽度比分段控件所需的宽度大得多。

Is there some way of getting the right button area of the navigation bar to simply be smaller? 有什么方法可以使导航栏的右按钮区域变小?

My steps are below: 我的步骤如下:

1.Drag the segment control to navigationbar: 1.将细分控件拖动到导航栏:

在此处输入图片说明

2.Change the segment width: 2.更改段宽度:

在此处输入图片说明

3.iPhone7 & iPhone 7Plus all looks good: 3,iPhone7和iPhone 7Plus看起来都不错:

在此处输入图片说明 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM