I'm trying to add UIToolbar to my keyboard with inputAccessoryView, but once the user tap the UITextField I get an Blank white screen or load White view if i put it in viewDidLoad(). I have tried few stackoverflow QA and non of them seems to be working for me. I'm also doing this with removeFromSuperview() method.
Initialization:
#IBOutlet var MessegeView: UIToolbar!
#IBOutlet var MessegeTextField: UITextField!
Assign UIToolbar to UITextField: This code result in blank white view when it get loaded.
override func viewDidLoad() {
super.viewDidLoad()
MessegeTextField.inputAccessoryView = MessegeView
MessegeView.removeFromSuperview()
}
WITHOUT removeFromSuperView(): I get the following error and that make sense kind of.
ERROR:
should have parent view controller:<APPNAME.ChatViewController:XXXXXXXX> but requested parent is:<UIInputWindowController: XXXXXXXXXX>
A few Stackoverflow QA I follow (but no result):
Error when adding input view to textfield iOS 8
Leaving inputAccessoryView visible after keyboard is dismissed iOS8?
How views are setup:
If you setup your UI in a Storyboard then you don't own the objects and shouldn't alter the hierarchy. That's why Xcode auto-generated properties from storyboards are #IBOutlet weak var by default, you don't own them and shouldn't manage the memory.
To fix your problem you should either use var MessegeView = UIToolbar() or you should initialize the toolbar from a separate .xib.
Related
ok, I have added a button to the .swift storyboard, I have then added the following code to my ViewController.swift
#IBOutlet weak var HelloButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
HelloButton.frame.origin.x = view.frame.size.width / 2
}
However when I test it on my iPhone 5s it moves it to the right of my screen and does not make it centered. I am unsure what I need to do to make the button centered.
Yes this is my first helloWorld app, and maybe over my head, but I understand web programing, and I understand to make it center automaticilly I need to have the screen view width total and the button width. I then need to say to the button to divide the screen view width by 2.
for example in CSS we do this
#button{margin:0 auto;}
but this is not HTML or CSS. IT'S SWIFT3
Try this:
HelloButton.center.x = view.frame.width/2
Yours is not correct because the origin.x is the first point (top left) of the button view, not the center of the view, you can change it's background color to different color to see it
Also, the variable name should be lowerCase, followed by code convention
You should be using Autolayout for accomplishing what you want and the way you do is like the following in your ViewController inside of Main.storyboard: (control + left mouse click -> dragging the blue line upwards and then releasing the control and left mouse click, you will see the popup)
In the popup select the options:
Center Horizontally In Container
Center Vertically in Container
But if you want to do it programmatically, do it in viewWillLayoutSubviews method:
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
override func viewWillLayoutSubviews() {
super. viewWillLayoutSubviews()
button.center = view.center
}
}
In your project Autolayout is enabled? if Auto Layout enabled then Change UI in viewdidLoad: may not give appropriate result. use -viewWillLayoutSubviews method to do UI task, it you want to center your button
myButton.center = self.view.center
Hope it will work.
Add the code to viewDidAppear that way the actual view has shown up. Also, you should use button.center.x instead of button.frame.origin.x if you want to center it
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 want to achieve the same flow as Facebook messenger app, with a tab bar controller inside the master view. See
Ive done exactly as described in this answer
Create a TabBar Controller with a Master-detail template?
However! It does not work correctly on iPhone, only on iPad. On iPhone, the navigation backwards does not work. The detail pane opens up just like a modal seque with no possibility of moving backwards. What could be the error here? Is this even possible to achieve with standard uisplitviewcontroller? I have tried embedding navigationcontroller in tabbarcontroller also (making navigationcontroller as root in master view), then it works for iPhone but not iPad.
I ended up getting around this by not using a UITabBarController, instead creating a CustomTabBarController which inherits from UIViewController. The custom controller has a UITabBar at the bottom of its view, and multiple other UIViewControllers embedded in Container Views. The custom controller sets the isHidden property to true for of all of the embedded view controller except the one which corresponds to the selected tab.
The following is a simple example with two tabs, identified by their tag:
class CustomTabBarController: UIViewController, UITabBarDelegate {
#IBOutlet weak var tab1View: UIView!
#IBOutlet weak var tab2View: UIView!
#IBOutlet weak var tabBar: UITabBar!
override func viewDidLoad() {
tabBar.delegate = self
}
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
tab1View.isHidden = item.tag != 1
tab2View.isHidden = item.tag != 2
}
}
This custom controller should be set to the root of a UINavigationController, which itself should be set as the master controller of the Split View Controller:
This setup works for both iPad and iPhone:
There are a few drawbacks to this method:
The custom tab controller is less easy to work with - adding a new tab requires that you add another embedded view and connect it to an outlet in the controller.
Setting the navigation item's title and left and right bar button items must be done within the custom tab bar controller, upon tab selection.
This method uses (I think) more memory than a regular UITabBarController, since all of the child view controllers are loaded as soon as the app loads, rather than when they are first shown.
This setup will lead to the tab bar being hidden when detail is shown in (portrait) iPhone mode. This was what I wanted, and is the behaviour in the Facebook Messenger app too, but if you want the tab bar to be permanently visible, this method won't do it.
I've been trying to implement Admob on swift and everything is good, but i run out of problem because I'm using custom table view cell and "Table view controller" as the main swift file, so when i am dragging the BannerView into the storyboard which is the table view controller, the ad actually appear in the first cell of the table view ! instead i need to load it at the button of the tableview controller not inside the cells ?
#IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
self.bannerView.rootViewController = self
var request: GADRequest = GADRequest()
request.testDevices = [GAD_SIMULATOR_ID]
self.bannerView.loadRequest(request)
// 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()
}
One solution could be to create a new instance of bannerView instead of having it as an outlet and then add it as a subview to the tableView's superview.
I had the same issue , i got it figured out by a slightly different method,
But it works like a charm.
So for all those Who are new to Swift and face this problem,
If Your using a story board, copy all your views( ex labels, UIImage's ) from the prototype cell
Go ahead and delete your View Controller. ( fear not , it will work eventually , trust me )
Drag a blank ViewController and in the class , mention your class associated with earlier tableView.
drag also tableview on it and selecting the tableview, put constraints form storyboard up & down-> 0 & left & right -> -20 .
drag a tableview cell, and selecting that cell, in class mention the cell class you were using for the earlier Tableview cell,
(tip-> You jus mite want to click on the module bellow the class and press enter, i got a stupid bug { unknown class in interface builder error , when i did it } )
now you can just select the cell and paste your earlier views in it and create a new connection with the cell class #IB outlets.
In the same Viewcontroller you can drag an empty UIView and make it a GADBannerView ( follow the Googles guide for admob iOS) Thats all in the IB
Now go to your tableview class , the class extension just make it to UIViewController , and confirm to protocols UITableViewDataSource & UITableViewDelegate.
ex: class Foo: UIViewController, UITableViewDataSource, UITableViewDelegate {
make a IB connection of your newly added UITableview in your main tableview (now viewController) class
#IBOutlet weak var Footableview: UITableView!
Now jus add these two lines in the viewDidLoad
Footableview.datasource = self
Footableview.delegate = self
Now just remove override form all tableview functions such as sections or RowForIndex functions, and wherever there is 'self.tableView' replace with 'self.Footableview'.
ScreenShot
Regards
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