Let me explain my problem here, I am working on a project in which I am having a xib file with a UIView. What I have done is, I created another UIView inside the same xib and I need to show that in my xib view in a button action using swift.
Note : If I tend to do this via programatically it is working fine but the problem while I am doing with my xib. (testview) is a view that I have created inside the xib.
class tableClass: UIViewController {
#IBOutlet var testview : UIView
override func viewDidLoad() {
super.viewDidLoad()
println(testview) **///// Return nil /////**
testview=UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) as UIView
println(testview) **///// Return nil /////**
self.view.bringSubviewToFront(testview)
self.view.addSubview(testview)
}
}
Thanks in Advance. Please let me know, your answers and valuable ideas.
I solved the above stated problem by initializing my UIView in ViewDidLoad method. Here, is the lines I wrote on my didLoad method
class tableClass: UIViewController {
#IBOutlet var testview : UIView = nil
override func viewDidLoad() {
super.viewDidLoad()
testview.frame = CGRectMake(0, 0, 0, 0)
self.view.addSubview(testview)
}}
After doing that, I can able to change the UIView frame anywhere from the class, like below. Happy Coding..
testview.backgroundColor = UIColor.greenColor()
testview.frame = CGRectMake(50, 50, 50, 50)
Related
This question already has an answer here:
Resize subview when superview finishes loading
(1 answer)
Closed 2 years ago.
I am manually creating the greenView in the viewDidload() method below and add it to miidLeView but its coordinates in the resulting screen is wrong? What is the reason for this? Where should add it to middleView? Thanks.
class ViewController: UIViewController {
#IBOutlet weak var redView: UIView!
var greenRegion : UIView!
let greenRegionHeight : CGFloat = 100.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
greenRegion = UIView(frame: CGRect(x: redView.bounds.size.width*1/8,
y: redView.bounds.size.height/2 - greenRegionHeight/2,
width: redView.bounds.size.width*3/4,
height: greenRegionHeight))
greenRegion.backgroundColor = UIColor.green
redView.addSubview(greenRegion)
}
You just need to put frame set related code into the viewDidLayoutSubviews method then everything worked as per expectation.
override func viewDidLayoutSubviews() {
greenRegion = UIView(frame: CGRect(x: redView.bounds.size.width*1/8,
y: redView.bounds.size.height/2 - greenRegionHeight/2,
width: redView.bounds.size.width*3/4,
height: greenRegionHeight))
}
Output:
I want to add a imageView below/down the TabBar in TabBarController is there any way to do that. I searched a lot got one answer about adding the TabBarController in other ViewController's container view and add that image down that container view. I also try to add image programmatically but it covers the TabBar.
So how can i do that any suggestion would be appreciated.
Thank You.
Create one custom class inherit it from UITabarController and use the following code
class CustomTabbarController: UITabBarController {
override func loadView() {
super.loadView()
let imageView = UIImageView(frame: CGRect(x: 0, y: self.view.frame.size.height - 10, width: self.view.frame.size.width, height: 10))
imageView.backgroundColor = UIColor.red // set image you wanted to show
self.view.addSubview(imageView)
}
override func viewDidLayoutSubviews() {
tabBar.frame.origin.y = self.view.frame.size.height - 60 // change it according to your requirement
}
}
Now set the custom class to the Tabbarcontroller inside storyboard
I am trying to make a function that returns a UITableView from a Cocoapod class. However, when I call the function, I get:
Thread 1: EXC_BAD_ACCESS (code=2, address=0x7fff89c111b0)
This does not happen when I initialize the table view within my main ViewController.
Eg
let tableView = UITableView()
This works correctly.
let tableView = laneScroller.makeTable()
However this fails.
Here is what my Cocoapod class does:
public class LaneScroller: NSObject {
private var size: CGFloat
public init(size: CGFloat) {
self.size = size
}
public func makeView() -> UIView {
return UIView(frame: CGRect(x: 0, y: 0, width: size, height: size))
}
public func makeTable() -> UITableView {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
tableView.delegate = self
tableView.dataSource = self
return tableView
}
}
I have implemented delegate and data source methods in extensions.
As you can see, I have used a similar approach to get a UIView and add it to my UIViewController's view and that works perfectly. I read somewhere that this might have something to do with weak and strong allocation. But not sure how to fix this.
you need to remove these two lines from function body
tableView.delegate = self
tableView.dataSource = self
and add it where function returns tableView instance because you implemened tableView datasource and delegates in UiViewController extension and Lanescroller is not inherited from UIViewController or I can say that it's not a view controller.
I've been looking for this for hours and have no luck yet. I want to use two custom views inside a scrollview. The first view have a button as a subview that leads the user to the next page(scrolls down). But the button action it's never fired. If I use the button as a scrollview subview everything works fine, but that's not what I want.
The code for the scrollview view controller:
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let redView = View1()
redView.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.scrollView.frame.height)
self.scrollView.addSubview(redView.view!)
let blueView = UIView(frame: CGRect(x: 0, y: self.scrollView.frame.height, width: self.view.frame.width, height: self.scrollView.frame.height))
blueView.backgroundColor = .blue
self.scrollView.addSubview(blueView)
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.scrollView.frame.height * 2)
}
func go(to page: Int) {
let y = CGFloat(page) * self.scrollView.frame.size.height
self.scrollView.setContentOffset(CGPoint(x: 0, y: y), animated: true)
}
}
ScrollView Storyboard Configuration
The code of the View1 Class:
class View1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func didTouchUpInsideMoreInfoButton(_ sender: Any) {
print("test")
}
}
Any ideas are welcome. Thanks in advance.
Turn out that I was using a view controller's view and for some reason, the selectors don't work this way. So, what I did was to create a UIView only and then everything works just fine.
Check if the buttons userInteraction is enabled.
Sometimes if your button is inside another view, a UIView for example, it doesn't allow you to tap it. To fix this, you might want to add the button ON TOP of that view but not inside it. Make sure it's above but not inside any view in on your storyboard.
I am not sure why this is happening.
In my application, I have
ViewController.swift
CustomUIView.swift
CustomUIView.xib
In my ViewController.swift
var customView: CustomUIView = NSBundle.mainBundle().loadNibNamed("CustomUIView", owner: self, options: nil).first as CustomUIView;
customView.setupControl();
self.view.addSubview(customView);
In my CustomUIView.swift I tried to change the properties of a UIButton created in the CustomUIView.XIB such as this
#IBOutlet weak var button: UILabel!
func setupControl()
{
label.frame = CGRect(x: 0 , y: 30, width: 320, height: 240); //THIS DOES NOT WORK
label.text = "BLABLABLA"; //THIS WORKS
}
I can only modify the property of the UIButton from the InterfaceBuilder.
Any thoughts on why this is happening?
Are you using Auto Layout? If yes, you can't set the frame, because the constraints will override the frame setting. To change the frame, you must set the constraints.