So I have been going through my code over and over and searching online for a solution to this problem and I am stuck. Nothing that I can find online has any information on why the screen would load and then turn black. The code modally presents another view that has a UIImageView inside of it. There is an image (.png file) that should be loading in the image view and you can see the image while the view is being presented modally, but as soon as the view slides into place the whole view turns black. Also you can see the image in the storyboard and it shows it correctly, it is only in the simulator where the image immediately turns black.
Here is the code of the view controller that is presented:
import UIKit
class MenuViewController: UIViewController
{
#IBOutlet var imageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view
let backgroundImage: UIImage! = UIImage(contentsOfFile: "behind_alert_view.png")
imageView.image = backgroundImage;
}
}
I also tried using the function UIImage(named: "behind_alert_view.png"). Any help would be greatly appreciated!
First, in your SecondViewController, set the main view's background color to clearColor, and add a second full-size view inside it. Set the second view's background color to your desired color, for example "darkGrayColor" and set it's alpha to 0.3.
Then, inside of your Interface Builder, click on the segue between FirstViewController and SecondViewController and under for "Presentation" choose "Over Current Context".
Alternatively, the following code should do the same thing. But in Xcode 6 Beta 7, I could not get it to work successfully:
self.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
Related
I want to add code to the back button presented in UINavigationController. But, I want the back button to look identical to what Apple presents.
I have this all setup to use my graphic in the leftBarButton, but am unable to get the graphic to look perfect. To that end, is there a way that I can use the internal iOS back button in my own custom button?
Unfortunately the navigation bar's back button is not very customizable. There is no simple way to access this image.
If you're familiar with Sketch or Photoshop, I suggest you take a screenshot of the back button and trace your image over the exact location.
If you can't do this, you technically can access the back buttons image through some minor UI manipulation. You'll have to first have a back button which is on screen. Once you know it's on screen, such as in your viewDidAppear, you'll want to look through the subviews of your leftBarButtonItem. One way you can do this is by calling navigationBar.subviews and navigating until the view. Another way is to expose the items target view.
extension UIBarButtonItem {
var targetView: UIView? {
guard let view = value(forKey: "view") as? UIView else {
return nil
}
return view
}
}
Now you can call leftBarButtonItem.targetView.subviews. Your for-loop would look something like,
for subview in leftBarButtonItem.targetView.subviews {
if let imageView = subview as? UIImageView {
self.image = imageView?.image
}
}
All of this is pseudo code and untested. Typically UIKit will use the standard classes (such as UIImageView) when building their views. However in older classes, they have been known to draw images manually. So if there is no image, you can always resort to taking a snapshot of the view with the arrow.
view.snapshotView(afterScreenUpdates: false)
Once you have your image / view, you'll save it in a property (most likely in your custom navigation controller) and then you'll have access to it whenever you push new view controllers.
First, an image:
I made it so you can drag the view controller view with your finger (which I have already done), but I want to know:
How to change the black color to another color
How I can put an image behind the view (I want to make it so if you drag the view you'll see a picture).
How do I do this? I figure I'll need to place another view directly behind this one maybe and then make make current view controlller a sub view?
You can set an image as the background of your ViewController by either changing the class of your ViewController's main view from UIView to UIImageView in Storyboard and setting the image to that ImageView's image property or by adding a UIImageView to the ViewController that has the same size as view.
By "background color", I think you mean the black color that shows when you drag the VC away, right?
That is the color of the UIWindow that you VC is running in. It's kind of the superview of every view.
To change the windows color, simply go to your AppDelegate.swift and change change it:
window?.backgroundColor = .red
To add a background image, you just need to add a UIImageView as a subview of window.
if let window = self.window
let imageView = UIImageView(frame: window.frame)
imageView.image = ...
window.addSubview(imageView)
}
If you don't want to deal with subviews and only use viewcontrollers, you can try to present a draggable viewcontroller over a normal (fixed position) viewcontroller. You can do it like this (call this code from the normal view controller to present the draggable view controller over itself):
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// Set the draggable controller's identifier in Main.storyboard
let dragController = storyboard.instantiateViewController(withIdentifier: "DragController")
// Present the draggable view controller over the current one
dragController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
dragController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(dragController, animated: true, completion: nil)
Use this code, and then set the background image of the normal view controller. Hope this helps :)
Hi I am making a simple login screen for my app in xCode and want to make sure that it fits any screen
import UIKit
class LoginController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// will allow me to put a image as my UI background instead of a color
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"BestBackground")!)
}
}
This successfully loads the image but i just want to make sure it will load on any screen size from iPhone 6 to iPhone 6 Plus
Don't set the background to a pattern. Just add a UIImageView behind all the other views. Set its contentMode to scaleAspectFill and set its image to your "BestBackground" image. You can do all of this is the storyboard or in code.
My question is technically a duplicate of this SO: How do I set custom navigation bar(title + titleView) for all navigation controllers and view controller?
The base class answer in the link above is my current solution as I will explain below. One problem with this solution though is that it makes my company logo image titleView disappear and reappear for a second with every segue, I am looking to keep the logo visible through all navigation transitions.
I am using a company logo in the center of my UINavigationBar. Currently, for any child UIView of my UINavigationViewController, I add a UINavigationItem outlet in the Xcode 7 Storyboard by CTRL Dragging to my Swift ChildViewController and always use the same ID:
#IBOutlet weak var uiNavItemTitle: UINavigationItem!
I then delete that above Xcode generated line because I have the same line in MyBaseViewController that ChildViewController inherits from. Now I can set the logo one time in MyBaseViewController and every ChildViewController inherits from MyBaseViewController, this code is in MyBaseViewController:
#IBOutlet weak var uiNavItemTitle: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
// MARK: - UINavigationController Defaults
uiNavItemTitle.titleView = CoreUtility.LogoForUINavBarGet()
self.navigationController?.navigationBar.topItem?.title = "";
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
}
What is becoming an issue with this solution is that for every ChildViewController in my UINavigationViewController stack of segues, I have to add a UINavigationItem outlet and make sure I inherit MyBaseViewController. Some of my navigation segue views deeper in the navigation chain should not really inherit from this MyBaseViewController though.
Another issue with my solution is that the company logo disappears and reappears a second later for each segue to a new UIViewController
I can create another level to the hierarchy of inheritance is one possible solution.
But my UINavigationItem.titleView is always the same company logo for every child UIViewController segued to.
Do I have to add a UINavigationItem to every UIViewController?
Or is there an "Application" level way such as using, in my AppDeletgate.application():
//MARK: - UINavigationBar.appearance
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.somethingForTitleView
Where somethingForTitleView becomes code to set a Application wide Navigation Bar titleView to my logo View.
I am building a help screen view controller of my app. It basically contains a UIWebView because I want to use HTML to display the help text. There is also another view controller which is kind of the "main" VC. There is a "Present as Popover" segue connecting the two.
From the IB, I see this:
I have an unwind segue that unwinds to the main VC. It will be triggered if the user taps the "Done" button.
And here is my code:
import UIKit
class HelpViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
automaticallyAdjustsScrollViewInsets = false
let htmlString = "You don't need to know about this."
webView.loadHTMLString(htmlString, baseURL: nil)
}
#IBAction func doneClicked(sender: UIBarButtonItem) {
performSegueWithIdentifier("exitToMain", sender: self)
}
}
And the unwind segues are working perfectly but the navigation bar is somehow very dark in colour!
I don't know what's happening here. I checked out this question
iOS 7 SDK - UI Navigation Bar is sometimes darker and sometimes not. But why?
The asker said that's because of a piece of code that he wrote but I swear I didn't use the same code as he did (from my limited obj-c to swift translation knowledge)!
Anyway, here is the relevant storyboard hierarchy thingy:
What causes this problem and how to fix it?
I solved the problem by setting the background colour of the UIWebView to white. And the navigation bar turns white!
I found this by observing how the colour changes when I scroll the web view up and down. I saw that initially it is kinda grey. But when I scrolled down to the white part of the web view, it changes to white. I deduced that the grey colour is actually the web view's background property and the navigation bar is kind of translucent.
And that's why setting the background colour to white fixes this issue.
Stretch the web view through the bottom of the navigation bar and set automaticallyAdjustScrollviewInsets to true. That would adjust the scroll view inset to show the content at the right content.
did you try this
self.navigationController!.navigationBar.translucent = true