ios - 使用 Swift 在子类中更改方向更改时更改自定义 View 的框架

标签 ios xcode uiview swift3 storyboard

我正在将自定义 UIView 子类的 nib 文件加载到我的 View Controller 中。这里的问题是,当我将方向更改为横向时, View 仍然显示在纵向框架中。我希望它的框架也设置为横向模式。

以下是我的代码:-

class CustomPopUpView: UIView {

@IBOutlet weak var vWtitle: UIView!
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var lblContent: UILabel!
@IBOutlet weak var vwAlertSub: UIView!
@IBOutlet weak var btnCenter: UIButton!

var view: UIView!

func xibSetup() {
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
    addSubview(view)
    lblTitle.adjustsFontSizeToFitWidth = true
    view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
    self.showAnimate()
}

func loadViewFromNib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "CustomPopUpView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
    return view
}

override func layoutSubviews(){
    super.layoutSubviews()
    print("LayoutSubViews...............")
   // view.frame = bounds
}

func showAnimate()
{
    self.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.alpha = 0.0;
    UIView.animate(withDuration: 0.25, animations: {
        self.alpha = 1.0
        self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    });
}

func removeAnimate()
{
    UIView.animate(withDuration: 0.25, animations: {
        self.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        self.alpha = 0.0;
    }, completion:{(finished : Bool)  in
        if (finished)
        {
            self.removeFromSuperview()
        }
    });
}


init(frame: CGRect, titleTxt:String, contentText: String, leftButtonText:String, rightButtonText:String) {
    super.init(frame: frame)

    xibSetup()
    lblTitle.text = titleTxt
    lblContent.text = contentText

    if leftButtonText.characters.count > 0 {
        btnLeft.isHidden = false
        btnLeft.setTitle(leftButtonText,for: .normal)
    }
    else {
        btnLeft.isHidden = true
    }
    if rightButtonText.characters.count > 0 {
        btnRight.isHidden = false
        btnRight.setTitle(rightButtonText,for: .normal)
    }
    else {
        btnRight.isHidden = true
    }
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}



@IBAction func btnCenterTapped(_ sender: Any) {
    self.removeAnimate()
}

我需要在 layoutsubviews 方法中再次设置框架吗?或者有没有其他方法可以改变 UIView 方向? (就像我们有 View Controller 的方向方法“viewWillLayoutSubviews”)

最佳答案

我认为自自动布局出现以来,再次设置框架不正确。
迅速 override func layoutSubviews() { super.layoutSubviews() self.setNeedsDisplay() } layoutSubviews() 是一个 View 方法。

或者您可以在中观察viewDidLoad NSNotificationCenter.defaultCenter().addObserver(self, selector: "functionThatYouWantTriggeredOnRotation", name: UIDeviceOrientationDidChangeNotification, object: nil)
或者,您可以只提供 viewWillTransition 的覆盖,而不必为 UIDeviceOrientationDidChangeNotification 添加观察者。

override func viewWillTransitionToSize(size: CGSize,
  withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
    if UIDevice.currentDevice().orientation.isLandscape {
            //do your thing
        }
}

关于ios - 使用 Swift 在子类中更改方向更改时更改自定义 View 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44237418/

相关文章:

ios - 如何在 UIStackView 中设置 UIView 的背景颜色?

ios - 如何减少 collectionview 中两个单元格之间的垂直空间?

xcode - 如何在 Swift 中对抛出函数进行单元测试?

ios - 如何修复上传 ipa 到 AppStore 时出现“"' CFBundleIconName' is Missing”错误?

ios - entry.remove(success, fail) 调用成功,但不删除图像

ios - 旋转后 UIView 坐标被交换但 UIWindow 不是?

ios - 设置 keyWindow rootViewController 在 iOS8 中不起作用

ios - 如何删除viewController中编码的 View 按钮?

ios - 使用约束以编程方式排列按钮

ios - 如何仅在我的 UIView 子类中设置所有属性后才调用函数?