分段控件在第一段显示奇怪的标题

时间:2017-09-13 17:59:13

标签: ios iphone swift3

我有一个分段控件。它在第一段显示出奇怪的头衔。虽然它有时并不总是发生,但我不知道哪一个触发了它。自动调整大小模式设置为与内容成比例 以下是iphone,故事板和自动布局约束的快照

Iphone

故事板 storyboard

Autolayout Constraints Autolayout constraints

我已扩展SegmentedControl以满足某些要求,这里是代码

class CustomSegmentedControl: UISegmentedControl {
    var current = 0
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        current = self.selectedSegmentIndex
        super.touchesBegan(touches, with: event)
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        if current == self.selectedSegmentIndex{
            self.sendActions(for: .valueChanged)
        }
    }
}

的ViewController

class MatchingViewController: UIViewController {

    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var segmentControl: UISegmentedControl!
    @IBOutlet weak var searchHolder: UIView!
    @IBOutlet weak var searchTextField: UITextField!
    @IBOutlet weak var searchButton: UIButton!

    enum tabEnum: Int {
        case all = 0
        case iceBreaker
        case tagged
        case connected
    }
    enum buttonTags: Int{
        case searchButton = 101
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTags()
        setupActions()
        setupSegmentControl()
        setupSearchBar()


    }


    func setupTags(){
        self.searchButton.tag = buttonTags.searchButton.rawValue
    }
    func setupActions(){
        self.searchButton.addTarget(self, action: #selector(self.didSelect(_:)), for: .touchUpInside)
    }
    func setupSearchBar() {
        self.searchButton.setTitleColor(UIColor.darkGreen(), for: [])
        self.searchHolder.layer.borderWidth = 1
        self.searchHolder.layer.borderColor = UIColor.darkGreen().cgColor
        self.searchHolder.layer.cornerRadius = 5
        self.searchTextField.delegate = self
    }

    func setupSegmentControl() {
        segmentControl.addTarget(self, action: #selector(self.didSelectTab(_:)), for: .valueChanged)
        didSelectTab(segmentControl)
    }



    func didSelectTab(_ sender: UISegmentedControl) {
        let value = sender.selectedSegmentIndex
        self.segmentControl.tintColor = UIColor.darkGreen()

        print("selectedIndex \(value)")
        let storyBoard = Global.sharedInstance.storyboard
        let storyBoardId = value == 0 ? "FirstViewControllerID" : "SecondViewControllerID"
        vc = storyBoard.instantiateViewController(withIdentifier: storyBoardId)

        if let vc = vc {
            vc.setValue(self, forKey: "parentController")
             vc.setValue(value, forKey: "tabIndex")
            self.containerView.subviews.forEach({ $0.removeFromSuperview() })
            self.containerView.addSubview(vc.view)
            vc.view.translatesAutoresizingMaskIntoConstraints = false
            addChildViewController(vc)
            NSLayoutConstraint.activate([
                vc.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
                vc.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
                vc.view.topAnchor.constraint(equalTo: containerView.topAnchor),
                vc.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
                ])
            vc.didMove(toParentViewController: self)

        }
    }


    func didSelect(_ sender: UIButton){
        if let tag = buttonTags.init(rawValue: sender.tag){
            switch tag {
            case .searchButton:
                self.searchTextField.endEditing(true)
                startSearch()
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

在幕后,Xcode为您的故事板构建localizable.strings文件,使用object-id将视图对象的字符串映射到条目。您显示的内容看起来像其中一个字符串的字符串名称。如果您编辑故事板的.strings文件并更改其中一个字符串映射(如"KkS-S7-SCw.segmentTitle" = "All"),那么您可能会得到您所看到的结果。我的猜测是对于Storyboard表面下的可本地化字符串的某种混乱。

答案 1 :(得分:2)

看起来像是SegmentedControl的“对象ID”(图像上的蓝色箭头)。 验证是否在Interface Builder中使用子类正确设置了“Class”属性(图像上的红色箭头)。

检查是否继续使用默认类问题。 如果它解决了问题,则错误应该在您的子类代码中。 如果没有,那就更奇怪了。

Class and Object ID properties locations