how to fit xib view width to superView width - ios

I have a xib view like this ,and its width is 728px now.
image1
iamge2
I apply it into my mainView like this (my mainView's width is 1024px),but its width doesn't fit my main view's width.
image3
image4
image5
what should I do? anyone can help me?
code
class common: UIView {
// コードから呼び出す際に使用される
override init(frame: CGRect) {
super.init(frame: frame)
loadNib()
}
// Storyboardから利用する際に使用される
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadNib()
}
// 上記どちらのinitからも使用される共通関数で、xibファイルを呼び出す。
func loadNib() {
// 第1引数のnameには、xibファイル名
// ownerはself固定
// optionsはここではnil
let view = Bundle.main.loadNibNamed("common", owner: self, options: nil)?.first as! UIView
view.frame = self.bounds
self.addSubview(view)
}
}

In your .xib file, give IBOutlet to view like this:
IBOutlet:
#IBOutlet var contentView: UIView!
You need to add autoresizingMask to the view you are loading.
Here, see the change in code.
Try below code for the resizing:
func loadNib() {
Bundle.main.loadNibNamed("common", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

Related

Swift scrollview adding from XIB not scrollable at all

I'm trying to import from Xib full screen ScrollView into my ViewController.
Following that guide i've made many working examples of it, but when importing it from Xib, ScrollView not responding on scroll (not even bouncing)
My Xib View class:
class TestScroll: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
self.translatesAutoresizingMaskIntoConstraints = false
}
public static func getViewFromNib() -> TestScroll {
return UINib(nibName: "TestScroll", bundle: .main).instantiate(withOwner: nil, options: nil).first as! TestScroll
}
}
And this is how i adding it in ViewController:
override func viewDidLoad() {
super.viewDidLoad()
let testScroll = TestScroll.getViewFromNib()
self.view.addSubview(testScroll)
}
Please help, i've checked many guides already, but not found working example with Xib.
You need to set a frame / constraints
let testScroll = TestScroll.getViewFromNib()
testScroll.frame = self.view.bounds
self.view.addSubview(testScroll)
leave this
self.translatesAutoresizingMaskIntoConstraints = false
only if you'll set constraints

Why is my SurperView unresponsive when I remove a UIView SubView?

I made a subview appear on my iOS app when a button is pressed. Then, I call a custom UIView from its class, menuView, and display that UIView on my main storyboard
The menuView appears fine on my Main Storyboard, but once I remove the subview, my SuperView (original view for main class for main view controller) is unresponsive, and I can't interact with anything.
What's wrong?
My Custom "menuView" UIView Class:
import UIKit
#IBDesignable class menuView: UIView {
var view:UIView!
#IBOutlet weak var label: UILabel!
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "menuView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
func xibSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
addSubview(view)
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
xibSetup()
}
}
Main View Controller Class:
var myMenuView:menuView!
#IBOutlet weak var menuButtonOutlet: UIBarButtonItem!
#IBAction func buttonPressed(sender: AnyObject) {
if menuButtonOutlet.title == "Menu" {
if (myMenuView != nil) {
self.myMenuView.view.removeFromSuperview()
}
self.myMenuView = menuView(frame: self.view.bounds)
self.myMenuView.alpha = 0.75
self.view.addSubview(myMenuView)
menuButtonOutlet.title = "Back"
} else {
self.myMenuView.view.removeFromSuperview()
menuButtonOutlet.title = "Menu"
}
}
Short answer:
self.myMenuView.view.removeFromSuperview()
should become
self.myMenuView.removeFromSuperview()
in both places you have this line in buttonPressed.
Explanation:
Your myMenuView is a container in which you place the view you instantiate from your xib. In buttonPressed you remove only this inner(xib) view inside myMenuView and not myMenuView itself. Thus myMenuView remains on screen and swallows all the touches.

How to put nib file in collection view in swift

I am one problem in use collection view.
How to put nib file in collection view?
I will create image slider in collection view.
Please help me.
#IBDesignable class Menu: UIView {
var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
func xibSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "Menu", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
etc etc ...
}
Then add a UIView to your Main.storyboard as with a Menu class in the Identity Inspector for that view.

AutoLayout not working with embedded Xib file

I have a nib with a label in it, which is centered vertically and horizontally using Auto Layout. I declared a subclass of UIView and loaded the nib file in it.
I then added an UIView to my main view controller and assigned the new subclass to it. The problem is that the label of the nib file is not centered to the view, it doesn't follow the AutoLayout constraints.
Download test project. Why is this happening?
Here's how you should write you custom class:
import UIKit
class CustomView: UIView {
#IBOutlet var view: UIView!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
view = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as! UIView
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
view.translatesAutoresizingMaskIntoConstraints = true
view.frame = bounds
self.addSubview(self.view)
}
}
Here's a working example:
Testxibautolayout2.zip
As you are loading the xib programmatically, it takes the original frame size of xib that is designed in storyboard which is 600x600. Make an outlet property of your custom view and change its frame after you load the xib! –
In your case you have to do it like:
import UIKit
class CustomView: UIView {
#IBOutlet var testView: UIView!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)
self.addSubview(self.testView)
self.testView.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
//or
self.testView.frame=self.bounds
}
}

Link a UIView subclass to xib file

I've created a UIView subclass, which i want to link to a xib file. I've added a xib file and set the class to DraggableView, however when i for instance create a lable and link it to the information. it returns that information is equal to nil? why doesnt it work?
class DraggableView: UIView {
var delegate: DraggableViewDelegate!
#IBOutlet var information: UILabel!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
information.text = "no info given"
information.textAlignment = NSTextAlignment.Center
information.textColor = UIColor.blackColor()
self.backgroundColor = UIColor.whiteColor()
}
}
Please ensure you add init from XIB method in your custom UIView like this:
class func instanceFromNib() -> UIView {
return UINib(nibName: "nib file name", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as UIView
}
And then you can use it like this:
var view = DraggableView.instanceFromNib()
view.information.text = "Test Label"
self.view.addSubview(view)
You need for your custom UIView to load XIB file first before setting objects value.
class DraggableView: UIView {
var delegate: DraggableViewDelegate!
#IBOutlet var information: UILabel!
init() { xibSetup() }
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if self.subviews.count == 0 {
xibSetup()
}
}
func xibSetup() {
let view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
addSubview(view)
// INIT THERE YOUR LABEL
information.text = "no info given"
information.textAlignment = NSTextAlignment.Center
information.textColor = UIColor.blackColor()
self.backgroundColor = UIColor.whiteColor()
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "nib file name", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
and call simplely like that :
var view = DraggableView()

Resources