ios - 如何将分段控件添加到导航栏? (iOS)

标签 ios swift xcode user-interface navigationbar

我想像这样将分段控件添加到导航栏 enter image description here

但是当我将分段控件拖到导航栏时,大标题消失了。如何创建上述 UI?

最佳答案

您应该将分段控件添加为导航栏的 titleView。 下面是示例代码:

let titles = ["All", "Missed"]
segmentControl = UISegmentedControl(items: titles)
segmentControl.tintColor = UIColor.white
segmentControl.backgroundColor = UIColor.blue
segmentControl.selectedSegmentIndex = 0
for index in 0...titles.count-1 {
    segmentControl.setWidth(120, forSegmentAt: index)
}
segmentControl.sizeToFit()
segmentControl.addTarget(self, action: #selector(segmentChanged), for: .valueChanged)
segmentControl.selectedSegmentIndex = 0
segmentControl.sendActions(for: .valueChanged)
navigationItem.titleView = segmentControl

关于ios - 如何将分段控件添加到导航栏? (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49744408/

相关文章:

ios - 使用SecCertificateCopyData时对象的潜在泄漏

ios - 初始化的枚举返回错误的 hashValue

ios - Google API Bug - 归因不一致

ios - NSOperationQueue 在响应 iOS 上再次运行相同的任务

ios - 自引用键上的 FMDB 级联

ios - 在 iOS 中无法使用我的 FacebookAppID

ios - ios中的六边形网格

ios - 如何设置@IBAction 来定位 UIView 子类

ios - iOS 中的 TCP 套接字编程。服务器客户端响应

swift - 使用 LSCopyApplicationURLsForBundleIdentifier 和 CFArrayGetValueAtIndex 获取应用程序的 URL/路径时出错