我正在将自定义UIView子类的nib文件加载到我的viewcontroller中。这里的问题是,当我将方向更改为横向时,视图仍然以纵向框架显示。我希望它的框架也可以在横向模式下设置。
下面是我的代码: -
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方向改变还有其他方法吗? (就像我们对viewcontroller的方向方法“viewWillLayoutSubviews”)
一样答案 0 :(得分:0)
此方法将帮助您抓取方向更改。因此,如果您想在横向更新某些内容以及您的笔尖高度或某些内容,请使用此方法更改代码。
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
}
}
Swift 3版本:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
}
答案 1 :(得分:0)
我认为自从布局出来后,再次设置框架不正确。
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
}
}
答案 2 :(得分:0)
Swift 4.2版本:
这是在UIView子类内部处理此问题的一种方法。
首先,您需要在UIView子类中设置观察者。
override init(frame: CGRect) {
super.init(frame:frame)
NotificationCenter.default.addObserver(self, selector: #selector(updateLayout), name: UIDevice.orientationDidChangeNotification, object: nil)
//UI setup
}
比起targer功能,您可以为自定义视图设置新框架。
@objc func updateLayout(){
if UIDevice.current.orientation.isLandscape {
self.frame = CGRect(.....)
} else {
self.frame = CGRect(......)
}
}
Dontr忘记在UIView的deinit方法中删除Obsserver。
deinit {
NotificationCenter.default.removeObserver(self,
name: UIDevice.orientationDidChangeNotification,
object: nil)
}