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.
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
Sorry in advance, this is a long question but I wanted to explain as good as I can.
I need to implement a walkthrough(first time launch guide) for the project, which will only be shown to user for the first time they launch the app.
I've implemented it by creating a .xib file, where I was just creating a view for each item I need to implement for the walkthrough by creating objects referring to .xib model.
But now I'm required to implement it without using .xib files, where I need to do it via storyboard. This time I've inserted a scrollView, and then I put another view inside it with the objects I'm going to need such as (labels,imageViews,buttons etc.) But it doesn't work, even though I can create all the objects by copying the view right under the scrollView. When I try to expand the width of the scrollView with all the views created so I can do paging, it doesn't create each view next to each other but only shows me the last object(view) in the simulator as they add up vertically, and I can't do no paging.
Now I provide some sample code to show how I try to implement this:
import UIKit
class DenemeViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var pageControl: UIPageControl!
#IBOutlet weak var repeatingView: UIView!
var viewArray = [UIView]()
override func viewDidLoad() {
super.viewDidLoad()
let temporaryView = repeatingView
temporaryView?.backgroundColor = .black
viewArray.append(temporaryView!)
let temporaryViewTwo = repeatingView
temporaryViewTwo?.backgroundColor = .blue
viewArray.append(temporaryViewTwo!)
pageControl.numberOfPages = viewArray.count
pageControl.currentPage = 0
view.bringSubview(toFront: pageControl)
setupScrollView(items: viewArray)
}
func setupScrollView(items: [UIView]) {
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(items.count), height: view.frame.height)
scrollView.isPagingEnabled = true
for i in 0..<items.count {
items[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(items[i])
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}
}
As you can see I first create two different views in viewDidLoad by referring to "repeatingView" which is the outlet of the view inside scrollView on the storyboard. Then I return them in an array and try to implement scrollView's subviews with views.
I was expecting to see the scrollView starting with the view with black background and as I do the paging through right then the view with blue background should appear.
When I run this what I see is only the blue view, and I'm unable to do any paging or scrolling.
I have a UIView called brokeView, It is draggable and rotational. I am trying to allow the user to populate the same exact view from clicking a button allowing an endless amount of the same UIView to populate. Is this possible to do in swift using the storyboard?
- Do I have to create all the views in storyboard or could I populate them through code using an array or loop?
#IBOutlet weak var brokeView: UIView!
#IBAction func showBrokenAnnotation(sender: AnyObject) {
brokeView.hidden = false
for i in 1...10 {
print(i)
let newView = UIView(frame: brokeView.bounds)
brokeView.addSubview(newView)
}
This can be done through code. Here is some pseudo code.
Updated from our conversation below - this makes it the same size as your "brokenView" and then you can set the x/y however you want.
let newView = UIView(frame: CGRect(x: 0, y: 0, width: mainView.frame.width, height: mainView.frame.height))
self.view.addSubview(newView)
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)