I am trying to move a picture in Xcode 8. I have the code for it, but the image won't move when the button is pressed. Can someone please help me solve the problem for the picture not moving.
import UIKit
class Level2ViewController: UIViewController {
var image = UIImage(named: "ballon.png")
var imageView = UIImageView(image: "ballon")//initialize properly, this is just for reference
#IBAction func buttonPressed(_ sender: UIButton) {
self.imageView.frame.origin.x += 50
}
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.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Delete your button from your code and then right click on your button and click on the "x" to remove the connection to the old code.
Now add the button back. I think your button was setup wrong.
when you right click the botton look and see what the Send Event was.
It should be setup this way.
If this is the answer you were looking for don't forget to except the answer.
Ok I think I see it.. You need to control drag your imageView to your code and connect it as an outlet and then move the imageView outlet. you are moving the variable.
If this is the answer you were looking for don't forget to except the answer.
Related
I am creating an app with Swift 4.0 and I am trying to delay the launch screen. The initial screen will sleep for 2 seconds, then the "a FusionPointInc application" label will disappear and the screen will stay at the red curtain background image (the second image). It will stay there and won't show the second screen (the Theater Stages Throughout History screen)
Here is my code for the initial screen:
override func viewDidLoad() {
super.viewDidLoad()
sleep(2)
showNavController()
}
func showNavController() {
performSegue(withIdentifier: "showMainView", sender: self)
}
More code for the initial screen (will transition to the second screen):
`class showMainView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//performSegue(withIdentifier: "showSegue", sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
This screen will open and when prompted to segue, the screen will only show the background (the second picture).
The middle screen code is in the ViewController.swift file and it has no actual code in it (just the pre-formatted base code)
I tested when I delete the initial screen, the app will load up fine. But, I wanted the initial screen to be delayed so "a FusionPointInc application" can be seen!
You are calling sleep on the main thread.
Instead, you should be doing something like this:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
self.showNavController()
}
}
This will call showNavController after two seconds without issues.
In my application I used SWRevealViewController in order to implement the side menu. There, for some reason I had to put a view which is embed in a navigation controller, before the reveal view controller.
This is my storyboard
Everything works fine other than one thing. when I drag from the left edge of the screen to the right side (pan gesture) on my home view, instead of side menu it navigates me to the previous view (in here case to the middle view which contains a button in the middle).
This is how it looks like when dragging
I want to avoid this and get the side menu when dragging like that. Can someone help me on this. Any help would be highly appreciated.
Edit:
This is the front_view and rear_view
Take a ViewController.swift file for the initial ViewController having button.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
There is no issue in the swrevealviewcontroller implementation but the native behavior of navigation controller is causing this issue.
You need to remove the popGesture from the navigation controller in any view controllers that you don't want to be able to use the swipe gesture:
var gestureRecognizer: UIGestureRecognizer? {
guard let nc = navigationController else { return nil }
return nc.interactivePopGestureRecognizer
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let gr = gestureRecognizer {
gr.isEnabled = false
}
}
To make this simple, because of the app design I cannot use an embedded navigation controller and must instead use a manual back button on the view controller to move back to the previous one. As far as I know, the previous view controllers push to this one programmatically as well. So I found some great code for a button on many different questions here, and I wrote it for this app. Problem is, when I run the program the Bar Button Item I created does not show up. The code shows no errors and I am confused as to why nothing is showing up. I'll include images and the code to make this as clear as possible. As always thank you for any help you can provide!!!
Code Used:
#IBOutlet weak var roomImage: UIImageView!
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var roomName: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
// Back Button Code
let backButton = UIBarButtonItem(title:"Back",style: UIBarButtonItemStyle.plain, target: self, action: #selector(DetailView.back(sender:)))
self.navigationItem.leftBarButtonItem = backButton
navigationItem.rightBarButtonItem = backButton
navigationItem.hidesBackButton = false
// Hide da tab bar
self.tabBarController?.tabBar.isHidden = true
// Do any additional setup after loading the view.
// Enter data from other view controllers
//roomName.title = stringPassed
roomImage.image = imagePassed
locationLabel.text = locationPassed
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
// Back Button Action
func back(sender: UIBarButtonItem) {
if let navigationController = self.navigationController {
navigationController.popViewController(animated: true)
}
}
}
Actual Storyboard with Navigation Bar
When the app is run
Could it be I didn't make something an outlet or something of that sort? Thank you!!
Storyboard layout
Tree Model of Detail View (The one with the navigation bar issue)
EDIT: Still not working but the links and ideas you gave have given me plenty of new ideas and options to explore until a more concrete answer presents itself. Thank you!!!
Remove the NavigationBar that you have drag from the Controls Library panel to your ViewController. When you set the navigationItem using self.navigationItem it will not reference to that navigationBar item because it has no connection with it.
When you push your ViewController using navigationController?.pushViewController(myVC, animated: true) it will automatically show NavigationBar on the myVC screen.
Can you show how did you push to DetialView Controller
I tired programmatically
like this
func showDetialViewController(){
let detialViewController = UIViewConroller()
navigationController?.pushViewController(detialViewController, animated: true)
}
I am working on setting up basic CRUD operations for a list using Table Views and Navigation Controllers. When I click the plus button in my initial Table View, it is supposed to use a segue to present modally the next Table View where I can add an item to the list in a text field that is in a table cell.
As it is now, when I click the plus button, it takes me to the right page, and the navbar buttons work on that page. When I click done, the text I have in my textField that I entered in the storyboard gets added to the list so I know it's there somewhere. I have auto layout set up so that shouldn't be the problem. It looks like there is something covering the table cell up (all darker grey), but I can't figure out what it is. See images below for clarification:
this image shows the storyboards, the last board is the one I having trouble with
this images shows the progression of the app as it is now, the middle frame should be showing the text field
Code for second view:
class MissionDetailsTableViewController: UITableViewController {
#IBAction func cancelBarButtonPressed(sender: UIBarButtonItem) {
cancelButtonDelegate?.cancelButtonPressedFrom(self)
}
#IBAction func doneBarButtonPressed(sender: UIBarButtonItem) {
delegate?.missionDetailsViewController(self, didFinishAddingMission: newMissionTextField.text!)
}
#IBOutlet weak var newMissionTextField: UITextField!
weak var cancelButtonDelegate: CancelButtonDelegate?
weak var delegate: MissionDetailsViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Protocols in second file:
protocol CancelButtonDelegate: class {
func cancelButtonPressedFrom(controller: UIViewController)
}
protocol MissionDetailsViewControllerDelegate: class {
func missionDetailsViewController(controller: MissionDetailsTableViewController, didFinishAddingMission mission: String)
}
I am still not sure what was going on but this worked for me:
I uncommented this default line of code in my second view controller, and saw that my table cell appeared. I recommented it out, and it still works.
self.navigationItem.rightBarButtonItem = self.editButtonItem()
import UIKit
class CollectionHolderViewController: MainPageContentViewController { //MainPageContentViewController inherits from UIViewController
var collectionViewController = UICollectionViewController()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionViewController = self.storyboard!.instantiateViewControllerWithIdentifier("TrophyRoom") as UICollectionViewController
// Do any additional setup after loading the view.
self.addChildViewController(collectionViewController)
self.view.addSubview(collectionViewController.view) //I tried collectionViewController.collectionView with no success either
self.collectionViewController.didMoveToParentViewController(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
This code worked for a UIPageViewController but not for this UICollectionViewController.
I just see a blank white (default) UIViewController which is expected if the UICollectionViewController's view was NOT set. There is no error shown in XCode 6.
Help?
Edit: The code DOES in fact work : ) Just a tip, set the collectionViewController.view to have a frame that fills the screen for consistency otherwise it will fit just below the status bar.