subview not clickable in UIViewController - ios

I'm trying to embed a custom view in UIViewController. But when displayed, it's not clickable since it's out of the UIView frame.
viewDidLoad:
#IBOutlet var theVC_InVC_Test: UIView!
#IBOutlet var TableView: UITableView!
override func viewDidLoad() {
TableView.delegate = self
TableView.dataSource = self
func embed(_ viewController:UIViewController, inView view:UIView){
viewController.willMove(toParent: self)
viewController.view.frame = view.bounds
view.addSubview(viewController.view)
self.addChild(viewController)
viewController.didMove(toParent: self)
}
embed(sidemenutest1(), inView: theVC_InVC_Test)
}
sidemenutest1 UIViewController:
func popItOver(){
let PopOverVC = UIStoryboard(name:"Main",bundle: nil).instantiateViewController(withIdentifier: "CoinsPopUp") as! CoinsPopUpViewController
self.addChild(PopOverVC)
PopOverVC.view.frame = UIScreen.main.bounds
self.view.addSubview(PopOverVC.view)
PopOverVC.didMove(toParent: self)
}
#IBAction func storeAction(_ sender: Any) {
popItOver()
}
It's displayed fine, but it's not clickable... When I tried to click the subview buttons, the TableView is clicked and not the subview.

You shouldn't add the popover menu as a container view controller. It should be presented from the controller as a modal view controller instead and set its background view color to be the translucent gray color.

Related

ios Container View change View

I have a containerView added to a View Controller. I am hoping to on swipe of the containerView change the view in the container. However when I use the following in my swipe action function it adds the view to the whole page not just changing the view inside the container.
class SwipeDateViewController: UIViewController, UIGestureRecognizerDelegate {
#IBOutlet weak var swipeContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func swipeLeftHandler(_ sender: UISwipeGestureRecognizer) {
let viewController = self.storyboard!.instantiateViewController(withIdentifier: "swipeViewControllerStoryboard") as! SwipeViewController
self.swipeContainer.addSubview(viewController.view)
}
}
How do I just change the view in the container and not update the whole screen?
I think maybe you could add modify function in your custom vc.
Then just run function of it.
For example:
var customVC:EmbeddedViewController?
func addView() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "EmbeddedViewController") as! EmbeddedViewController
self.addChild(vc)
vc.view.bounds = CGRect.init(x: 20, y: 40, width: 50, height: 60)
self.view.addSubview(vc.view)
vc.didMove(toParent: self)
customVC = vc
}
#IBAction func actionAddView(_ sender: Any) {
customVC?.changeColor(color: UIColor.black)
}
EmbeddedViewController
class EmbeddedViewController: UIViewController {
public func changeColor(color:UIColor) {
self.view.backgroundColor = color
}
}

Add a view controller as a subview in another view controller

I have already read this LINK , but not working for me. I want to show a viewController as a subview in another viewController.
Here is my code -
import UIKit
import CarbonKit
class ViewController: UIViewController, CarbonTabSwipeNavigationDelegate {
#IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let items = ["All", "WOMEN", "MEN", "KIDS", "HOME", "CITY"]
let carbonTabSwipeNavigation = CarbonTabSwipeNavigation(items: items, delegate: self)
carbonTabSwipeNavigation.insert(intoRootViewController: self)
}
func carbonTabSwipeNavigation(_ carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAt index: UInt) -> UIViewController {
// let screen = self.storyboard?.instantiateViewController(withIdentifier: "demo") as! demo
// showSubViewContrller(subViewController: vc)
// return screen
let storyBoard = getStoryBoardByIndentifier(identifier: "All")
let vc = storyBoard.instantiateViewController(withIdentifier: "AllViewController") as! AllViewController
showSubViewContrller(subViewController: vc)
return vc
}
//Subview Controller
func showSubViewContrller(subViewController:UIViewController) {
self.addChildViewController(subViewController)
subViewController.view.frame = containerView.frame
self.containerView.addSubview(subViewController.view)
subViewController.didMove(toParentViewController: self)
}
func getStoryBoardByIndentifier(identifier:String)->UIStoryboard {
return UIStoryboard.init(name: identifier, bundle: nil)
}
}
I have a NavigationBar and a tapBar. Would like to show the viewController inside the view in a container.
But when the view loads it's coverUp/hide the tabBar.
How to solve this and show the viewController in my specified container.
Project Link - GitHub
Somehow i am able to fix your issue with below changes:
Replace this method carbonTabSwipeNavigation.insert(intoRootViewController: self) with carbonTabSwipeNavigation.insert(intoRootViewController: self, andTargetView: containerView) in viewDidLoad
Note : Give UITaBar bottom constraint to SuperView not SafeArea:
Add below code in ViewController:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tabbar.invalidateIntrinsicContentSize()
}
After doing this when you run you will UITabBar:

How to center the popupview that contain dynamic height content ??

I am now implementation the pop up dialog for my iOS app according to this youtube video "https://www.youtube.com/watch?v=FgCIRMz_3dE".But the problem is I can't set my pop up view to fixed height because there is dynamic height label in my popup view which is inside my popup view controller.Can anyone tell me how to solve this solution?Thanks for your attention.
Here is my code to open popup view controller from parent view controller.
let PopUpVC = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "FeedPopUpViewController") as! FeedPopUpViewController
self.addChildViewController(PopUpVC)
PopUpVC.view.frame = self.view.frame
self.view.addSubview(PopUpVC.view)
PopUpVC.didMove(toParentViewController: self)
Here is my code for FeedPopUpView Controller
import UIKit
class FeedPopUpViewController: UIViewController {
#IBOutlet weak var action_Label: UILabel!
#IBAction func dismiss(_ sender: Any) {
print("pop up is dismissed")
self.view.removeFromSuperview()
}
override func viewDidLoad() {
self.showAnimate()
super.viewDidLoad()
print("pop up is created")
}
override func viewWillAppear(_ animated: Bool) {
print("pop up is appeared")
}
func showAnimate(){
}
}
PopUpVC.view.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
This should do the work :)

How can I swipe between view controllers?

I want to add a swipe action to my app. Basically I have 5 view controllers and I main view controller. On my main view controller I have a view and I am calling the content from other 5 view controllers to that view. And I want to swipe those 5 view controllers.
My code:
import UIKit
class TabViewController: UIViewController {
#IBOutlet var contentView: UIView!
#IBOutlet var buttons: [UIButton]!
#IBOutlet var backgroundView: UIImageView!
var movingView = UIView()
var rifleViewController: UIViewController!
var pistolViewController: UIViewController!
var shotgunViewController: UIViewController!
var smgsViewController: UIViewController!
var sniperViewController: UIViewController!
var viewControllers: [UIViewController]!
var selectedIndex: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
rifleViewController = storyboard.instantiateViewController(withIdentifier: "rifles")
sniperViewController = storyboard.instantiateViewController(withIdentifier: "snipers")
smgsViewController = storyboard.instantiateViewController(withIdentifier: "smgss")
shotgunViewController = storyboard.instantiateViewController(withIdentifier: "shotguns")
pistolViewController = storyboard.instantiateViewController(withIdentifier: "pistols")
viewControllers = [rifleViewController,
pistolViewController,
shotgunViewController,
smgsViewController,
sniperViewController]
buttons[selectedIndex].isSelected = true
didPressTab(buttons[selectedIndex])
let screenWidth = UIScreen.main.bounds.width
movingView = UIView(frame: CGRect(x: 0, y: 80, width: screenWidth / 5, height: 5))
movingView.backgroundColor = UIColor.white
backgroundView.addSubview(movingView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func didPressTab(_ sender: UIButton) {
let previousIndex = selectedIndex
selectedIndex = sender.tag
buttons[previousIndex].isSelected = false
let previousVC = viewControllers[previousIndex]
previousVC.willMove(toParentViewController: nil)
previousVC.view.removeFromSuperview()
previousVC.removeFromParentViewController()
sender.isSelected = true
let vc = viewControllers[selectedIndex]
addChildViewController(vc)
vc.view.frame = contentView.bounds
contentView.addSubview(vc.view)
vc.didMove(toParentViewController: self)
let newx = sender.frame.origin.x
UIView.animate(withDuration: 0.2) {
self.movingView.frame.origin.x = newx
}
}
}
You must go for UIPageViewController.
With the setViewControllers(_:direction:animated:completion:) you can set an array of your view controllers and also you can customise the animation.
Add on all of your viewController two Swipe Gesture Recognizer. Take care that you really drag and drop them onto the View Controllerin the hierarchy because else it might not work (For the most left and most right view controller you just have to add one swipe gesture recognizer). After that change for one of the the Swipe Gesture Recognizerper view controller the Swipe option in the attribute inspector from the standard Left to Right.
Create from each view controller to the next a Showseague and one back. Give them an Identifier you can remember. Then write something like that into each ViewController.swift and link the #IBAction with the two Swipe Gesture Recognizer of each corresponding view controller:
#IBAction func didSwipe(_ sender: UISwipeGestureRecognizer) {
if sender.direction == UISwipeGestureRecognizerDirection.left {
performSegue(withIdentifier: "identifierOfSegue", sender: nil)
}
if sender.direction == UISwipeGestureRecognizerDirection.right {
performSegue(withIdentifier: "identifierOfOtheSegue", sender: nil)
}
}
I don't know if I understood your question, but you can add (from Interface Builder) a UIGestureRecognizer (for the swipe action) to the View and in the selector of that gesture, you present the View you'd like to show.

Swift 2 - change UIView background colour from another UIViewControl

I am trying to change the colour of a UIView in View Controller when a button is pressed in Collection View Controller.
In my UIViewController I have a function that changes the color when this function is called from collection view controller the function prints out but the colour is not changed.
View Controller
static let sharedInstance = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("settingPageId") as! SettingsViewController
#IBOutlet weak var TopView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let colorsViewController = storyboard?.instantiateViewControllerWithIdentifier("colorsViewCollection")
self.addChildViewController(colorsViewController!)
colorCollectionView.addSubview((colorsViewController!.view)!)
}
func colorHasBeenSelected(){
TopView.backgroundColor = UIColor.blackColor() // color is not changed if called from collection view
print("colorHasBeenSelected") // it prints ok
}
Collection View Controller
let settingPage = SettingsViewController.sharedInstance
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
_ = settingPage.view // without it TopView returns nil
}
#IBAction func colorAction(sender: UIButton) {
settingPage.colorHasBeenSelected()
}
How can I make the UIView inside UIViewController change the background colour from the UICollectionView?

Resources