简体   繁体   English

iOS 9 标题视图未正确对齐

[英]iOS 9 title view is not correctly aligned

my UISegmentedControl in the centre of navbar looks fine in all iOS versions except For IOS 9. I have looked at many discussions but I couldn't find a solution.我在导航栏中心的 UISegmentedControl 在所有 iOS 版本中看起来都很好,除了 IOS 9。我看过很多讨论,但我找不到解决方案。

This happened at iOS 9这发生在 iOS 9这发生在 iOS 9

It should look like this它应该看起来像这样

应该是这样的

I added the segmented control this way:我以这种方式添加了分段控件:

listToggle = UISegmentedControl(items: [title1, title2])
self.navigationItem.titleView = listToggle

I also tried to use a UIView to wrap the toggle inside it and then show it, didn't work.我还尝试使用 UIView 将切换开关包裹在其中然后显示它,但没有用。 making the segmented control smaller does not work neither.使分段控件更小也不起作用。

the problem with my UISegmentedControl was that originally I defined some constraints for height and width of the segmentControl.我的 UISegmentedControl 的问题在于,最初我为 segmentControl 的高度和宽度定义了一些约束。 but this approach is wrong when you want to assign a segmented control to titleView on navBar.但是当您想将分段控件分配给 navBar 上的 titleView 时,这种方法是错误的。 you should let the titleView itself decide for the height and width of the segmented control.您应该让 titleView 自己决定分段控件的高度和宽度。

after all it was working fine on all other iOS versions except for iOS 9 and on a very old device.毕竟它在所有其他 iOS 版本上运行良好,除了 iOS 9 和非常旧的设备。 but it was a good practice to know how title view works.但了解标题视图的工作原理是一个好习惯。

the other notable thing I learned after this was that you should make sure segmentedControll has a frame before you pass it to titleView and that's all.在此之后我学到的另一件值得注意的事情是,在将 segmentedControll 传递给 titleView 之前,您应该确保它有一个框架,仅此而已。

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

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