1
votes

app crashing after upgrading project to xcode 9 and swift 4. while debugging at

    backview.addsubview(stackview)

app crashing with libc++abi.dylib: terminating with uncaught exception of type NSException. code is running perfectly in swift 3. But after migrating to swift 4 it crashed. This is my whole function.

    var isAlreadyThere = false

    if let audioView = self.view.subviews.filter (
        { $0.tag == 123456}).first  {

        audioView.tag = 123456
        isAlreadyThere = true
    }

    if isAlreadyThere {
        return
    }

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
    let backView = UIVisualEffectView(effect: blurEffect)
    backView.tag = 123456
    backView.frame = CGRect(x: 0, y: self.view.frame.height+35 , width: self.view.frame.width, height: 40)
    backView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(backView)

    pauseButton = UIButton(type: .custom)
    pauseButton.titleLabel?.font = Util.getFontSize()
    pauseButton.setTitle("Pause", for: .normal)
    pauseButton.setTitleColor(UIColor.black, for: .normal)
    pauseButton.addTarget(self, action: #selector(pausePressed(_:)), for: .touchDown)

    durationLabel = UILabel()
    durationLabel.textColor = UIColor.black
    durationLabel.font = Util.getFontSize()
    durationLabel.textAlignment = .center
    durationLabel.text = "0:00"

    stopButton = UIButton(type: .custom)
    stopButton.titleLabel?.font = Util.getFontSize()
    stopButton.setTitle("Stop", for: .normal)
    stopButton.setTitleColor(UIColor.black, for: .normal)
    stopButton.addTarget(self, action: #selector(stopPressed(_:)), for: .touchDown)

    let stackView = UIStackView(frame: CGRect(x: 0, y:0 , width: backView.frame.width, height: 40))
    stackView.alignment = .fill
    stackView.axis = .horizontal
    stackView.distribution = .fillEqually
    stackView.spacing = 5
    stackView.addArrangedSubview(pauseButton)
    stackView.addArrangedSubview(durationLabel)
    stackView.addArrangedSubview(stopButton)

    backView.addSubview(stackView)

    if audioViewYposition == nil {
        audioViewYposition = self.view.frame.height - 40
    }

    UIView.animate(withDuration: 0.2, delay: 0.1, options: .curveEaseInOut, animations: {
        backView.frame.origin.y = self.audioViewYposition
    }, completion: nil)
1
Try adding an exception breakpoint and see where the crash happens.badhanganesh

1 Answers

1
votes

As stated on error you need to add a subview on content view and not directly on visual effect view so the solution is:

backView.contentView.addSubview(stackView)

Make sure if your exception breakpoint is enabled to skip a few frames from breakpoint to see the error message in console:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: '<UIStackView: 0x109946ff0; frame = (0 0; 375 40); layer = <CATransformLayer: 0x1c4236640>> 
has been added as a subview to 
<UIVisualEffectView: 0x109945f40; frame = (0 702; 375 40); autoresize = W+H; tag = 123456; layer = <CALayer: 0x1c002ebc0>>. 
Do not add subviews directly to the visual effect view itself, 
instead add them to the -contentView.'