使用Swift更改子类中方向更改的自定义视图框架
我将自定义UIView子类的nib文件加载到我的viewcontroller中。这里的问题是,当我将方向改为横向时,视图仍以纵向框架显示。我希望它的框架也被设置为横向模式。使用Swift更改子类中方向更改的自定义视图框架
下面是我的代码: -
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方向改变? (就像我们对视图控制器的方向方法“viewWillLayoutSubviews”)
这种方法将帮助你抓取方位改变。所以如果你想更新景观中的某些东西,比如你的笔尖高度或其他东西,用这个改变你的代码方法。
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("Landscape")
//Change your code here for landscape
} else {
print("Portrait")
//Change your code here for portrait
}
}
斯威夫特3版本:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
}
这是一个UIView方法吗?我可以在此设置一个框架 – mirza
不,这是一种视图控制器方法。所以你需要将你的框架改为你使用的视图控制器。 –
我想设置帧再一次不正确的,因为自动布局走了出来。 in swift override func layoutSubviews() { super.layoutSubviews() self.setNeedsDisplay() }
因为layoutSubviews()是一个查看方法。
,或者你可以观察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
}
}
如何将CustomPopUpView添加到视图控制器?显示这些代码。 –
我不想写关于这个视图的任何代码在viewcontroller中调整大小 – mirza