Show different Views based on conditions on button click in Swift 3? - swift3

I have a ViewControllertheir is a button at top right side. When i click that button i want to Show/Hide a UIView at bottom with size half of ViewControlleron same ViewController . Anyone help me. Thanks in advance.

If only Show/Hide a UIView is your requirement, below code will work:
class ViewController: UIViewController
{
#IBOutlet weak var viewHeightConstraint: NSLayoutConstraint!
#IBOutlet weak var customView: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.viewHeightConstraint.constant = self.view.bounds.height / 2
}
#IBAction func onShowHideButtonPress(_ sender: UIButton)
{
self.customView.isHidden = !self.customView.isHidden
}
}
Storyboard:

Related

Push UIViewController from SwiftUI with UINavigation with Back Button?

I need to open UIViewController from SwiftUI with UINavigation having back button.
Currently, I had implemented this via ViewControllerRepresentable and NavigationLink, its works, but I have two Navigations SwiftUI(NavigationLink with back button) and default UINavigation without back button, and I would like to perform to have my default UINavigation with the back button and hide SwiftUI(NavigationLink) navigation.
struct UIKitVc : UIViewControllerRepresentable {
let navigationController = UINavigationController()
func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
}
func makeUIViewController(context: Context) -> UINavigationController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "SearchResult") as! SearchResultViewController
self.navigationController.addChild(viewController)
return self.navigationController
}
}
NavigationLink(destination: UIKitVc(){}
Thank you in advance.
If you're going to use NavigationView/NavigationLink then you don't need to use UINavigationController, just use representable over your UIViewController, like
struct UIKitVc : UIViewControllerRepresentable {
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
func makeUIViewController(context: Context) -> UIViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier:
"SearchResult") as! SearchResultViewController
// ... make additional set up here if needed
return viewController
}
}

Unlapping optional error in UIView

I wanted to programmatically control the menu view so I tried the following:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var menuView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func menuBtn(_ sender: UIButton) {
print("menuBtn")
print(type(of: menuView!)) // <- `Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)`
}
}
And click the button, console says
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
Both are connected to ViewController.
How can I get print(type(of: menuView!))?
===============================================================
ps.
I've simplified my real problem. this is my real code:
button on ViewController.Swift
#IBAction func openModal(_ sender: UIButton) {
print("open btn clicked")
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "menuViewController") as! MenuViewController
self.addChildViewController(vc)
self.view.addSubview(vc.contentView!) /// <- This cause problem
}
MenuViewController.swift
import UIKit
class MenuViewController: UIViewController {
#IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
view.isOpaque = false
}
#IBAction func closeBtn(_ sender: UIButton) {
print("close btn clicked")
self.view.removeFromSuperview()
}
}
and Why I wan't using it:
I can't scrolling when menuView appeared.. I want to use the buttons in the menu view and scroll at the same time.

UITextField.textColor not called until UITextField gets focus

Could anybody explain please how/when .textColor is called for the UITextField?
XCode9, Swift 4
Consider the following code:
import UIKit
class TestViewController: UIViewController {
#IBOutlet weak var txt1: UITextField!
#IBOutlet weak var txt2: UITextField!
#IBAction func btnaction(_ sender: UIButton) {
txt1.textColor=UIColor.red
txt1.backgroundColor=UIColor.green.withAlphaComponent(0.5)
txt2.textColor=UIColor.red
txt2.backgroundColor=UIColor.green.withAlphaComponent(0.5)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Everything is connected. When you run it and click on the button, only the backgroundColor gets set immediately. The .textColor remains the same. Only when the text field gets a focus, only then the .textColor changes.
Is this correct behaviour or a bug?
Thank you for comments.
I tried the same code in XCode 8, Swift 3 and it worked fine, so I believe it is probably a bug.

Can't click button inside UITableViewCell?

I'm using storyboard to create a UITableView, loading up some data to recommend users to follow, and including a follow button inside the cell.
Only problem is, I'm not able to click the follow button inside the cell.
Here's the code for the TableView in my ViewController class (It isn't a TableViewController, it's a UIViewController that includes UITableViewDelegate and UITableViewDataSource):
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return followRecommendations.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let followCell = tableView.dequeueReusableCell(withIdentifier: "followCell", for: indexPath) as! FollowRecommendationTableViewCell
followCell.followButton.tag = indexPath.row
followCell.followButton.addTarget(self, action: #selector(self.buttonTapped), for: UIControlEvents.touchUpInside)
followCell.followRecommendationName.text = followRecommendations[indexPath.row].suggestedUserName
followCell.followRecommendationInterests.text = followRecommendations[indexPath.row].suggestedUserTasteString
let imageUrl = followRecommendations[indexPath.row].suggestedUserImage
followCell.followRecomendationImage.downloadedFrom(link: imageUrl)
return followCell
}
func buttonTapped() {
print("Tapped")
}
and here's the code for the TableViewCell class.
class FollowRecommendationTableViewCell: UITableViewCell {
#IBOutlet weak var followRecomendationImage: UIImageView!
#IBOutlet weak var followRecommendationName: UILabel!
#IBOutlet weak var followRecommendationInterests: UILabel!
#IBOutlet weak var followButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
I've seen this question on StackOverflow before, but I wasn't able to fix the problem. Here are some things I've tried:
Numerous variations of enabling/disabling user interaction for the tableView, cell, content view, and button.
Moving the button to the front of the content view using this code: followCell.bringSubview(toFront: followCell.followButton)
I've tried the delegate/protocol method shown in this tutorial: http://candycode.io/how-to-properly-do-buttons-in-table-view-cells/
I just can't seem to get it to work.
Has anyone else had this issue and figured out a solution?
Thanks in advance!
I checked - your code works fine. You probably don't have IBOutlet set for your button on the cell, either in Storyboard, (or in separate .xib file if you use it). Open your storyboard and match the outlet from the cell to button, like this:

SWIFT : Display label

I begin to learn swift language and i've a problem.
I try to display a label when i put the button. I show you :
#IBAction func nextRule(sender : UIButton) {
rule = Rules(game: Int(randomNumber))
}
#IBOutlet var lTitle: UILabel!
#IBOutlet var lRule: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
lTitle.text = rule.title
lRule.text = rule.rule
Rules(game: Int) contain a model whith "title" And "rule"
When i press the button, i want to display a new Rule, is it possible by doing something like his?
Thanks for your answer.
How about assign your rule to lTitle and lRule like this
#IBAction func nextRule(sender : UIButton) {
rule = Rules(game: Int(randomNumber))
lTitle.text = rule.title
lRule.text = rule.rule
}