Is it possible to set the viewController for an UIView programmatically? What I want to do is make a UIView that always covers 50% of the screen's height and add a ViewController on that view after initializing it. I can't seem to find a method. Pseudocode:
let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height UIScreen.main.bounds.height * 0.5)
//Can't seem to find a method that does exactly this:
view.setViewController(.......)
Insert yourAnotherVC.view as a subview to yourview
var MenuView: AnotherViewController? = (self.storyboard?.instantiateViewController(withIdentifier: "NewsView") as? AnotherViewController)
self.addChildViewController(MenuView)
MenuView?.didMove(toParentViewController: self)
MenuView?.view?.frame = CGRect(x: CGFloat(0.0), y: CGFloat(self.containerView.frame.size.height), width: CGFloat(self.containerView.frame.size.width), height: CGFloat(self.containerView.frame.size.height))
yourview.insertSubview(MenuView?.view, belowSubview: self.bottomMenuView)
//Show presentation logic here forward i mean show Anotherviewvc bottom to top or top to bottom
I'll hide and show views instead of switching out the whole UIViews so no more answers needed #close
// Add Child View Controller from your MainVC
addChildViewController(viewController) //your 2nd VC
// Add Child View as Subview
view.addSubview(viewController.view)
// Configure Child View
viewController.view.frame = view.bounds //here you make it 50% of height!
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Notify Child View Controller
viewController.didMove(toParentViewController: self)
You can add a Container View, that uses its own UIViewController. Simply drag it from the object library and you are good to go.
Related
I want to add a imageView below/down the TabBar in TabBarController is there any way to do that. I searched a lot got one answer about adding the TabBarController in other ViewController's container view and add that image down that container view. I also try to add image programmatically but it covers the TabBar.
So how can i do that any suggestion would be appreciated.
Thank You.
Create one custom class inherit it from UITabarController and use the following code
class CustomTabbarController: UITabBarController {
override func loadView() {
super.loadView()
let imageView = UIImageView(frame: CGRect(x: 0, y: self.view.frame.size.height - 10, width: self.view.frame.size.width, height: 10))
imageView.backgroundColor = UIColor.red // set image you wanted to show
self.view.addSubview(imageView)
}
override func viewDidLayoutSubviews() {
tabBar.frame.origin.y = self.view.frame.size.height - 60 // change it according to your requirement
}
}
Now set the custom class to the Tabbarcontroller inside storyboard
I have a UIViewController that acts as a Container View Controller. It has a UIScrollView that has the same width as the screen, but it's height is smaller than the height of the screen.
The UIScrollView contains the views of two other UIViewControllers and those views can be horizontally scrolled through.
I set my contentSize like this:
scrollView.contentSize.width = self.view.bounds.width * 2
This works and allows me to scroll through my UIViewController views horizontally.
The following is how I add the UIViewController views to my scrollView:
private func addPanel(viewController: UIViewController, panel: Panel) {
let xOffset: CGFloat!
switch panel {
case .left:
xOffset = 0.0
case .right:
xOffset = self.view.bounds.width
}
let panelView = UIView(frame: CGRect(x: xOffset, y: 0, width: self.view.bounds.width, height: self.scrollView.bounds.height))
scrollView.addSubview(panelView)
let panelViewController: UIViewController! = viewController
var viewBounds = view.bounds
viewBounds.height = panelView.bounds.height
panelViewController.view.frame = view.bounds
panelView.addSubview(panelViewController.view)
addChildViewController(panelViewController)
panelViewController.didMove(toParentViewController: self)
}
For some reason, the UIViewController views don't resize to fit the height of the UIScrollView.
Should I be doing it constraint based? How would I do this. I've been struggling with this for a few days and am at a loss.
Essentially, the UIViewController views will just like like full screen views offset and so you can't see the bottom of the views because the bottom of the screen cuts them off.
I'm just taking a guess without knowing all the code, but one reason could be if you're adding the child view controllers before the scrollview has been layouted.
After you add and set the child views sizes, the scrollview adjusts its size to the phone size but you never update the child views.
My suggestion here would be to add the child view controllers to the scrollview, but move the frame setting code into a layouting method where you know your views have the correct(visible) frames/bounds.
Given you are writing this in a view controller, one method could be -viewDidLayoutSubviews
Hope this makes sense, feel free to ask more questions if it doesn't.
I am trying to place a UIView - popupView at the top, and another UIView (opaqueView) below popupView but above anything else. PopUpView is connected with an Outlet.
func display() {
popupView.center = CGPointMake(CGRectGetMidX(self.view.bounds), tableView.center.y);
self.view.addSubview(popupView)
popupView.clipsToBounds = true
let opaqueView = UIView()
let screenSize: CGRect = UIScreen.mainScreen().bounds
opaqueView.frame.size = CGSize(width: screenSize.width, height: screenSize.height)
opaqueView.alpha = 0.6
UIApplication.sharedApplication().keyWindow!.insertSubview(opaqueView, belowSubview: popupView)
}
Using this approach causes the opaqueView getting placed over everything including the popupView. Instead, I want to have popupView above opaqueView but keep opaqueView above everything else (view, TabBar, NavBar)
parent.insertSubview(child, belowSubview: sibling) works only when the sibling is a direct child of parent, so that child and sibling share the same parent. The current code does not work because opaqueView (the child) and popupView (the sbiling) have different parents.
That means either ① popupView should use the keyWindow as the parent, or ② opaqueView should use self.view as parent. Since you want opaqueView be above of everything, option ① is the only solution.
i have a scrollview with some subviews. Inside this subviews i have other subviews.
i want to present a popover that is anchored to one of this "nested" subviews.
I correctly show the popover, and it is anchored to the corrected subview, but whenever i scroll the scrollview, the popover doesn't move with the scrollview.
I would like my popover to move and to adjust its "x" origin every time i scroll the scrollview horizontally.
This is the code i use to present the popover.
func showAlarmViewController(notification: NSNotification){
troubleViewController = TroubleshootViewController()
troubleViewController!.modalPresentationStyle = .Popover
troubleViewController!.preferredContentSize = CGSizeMake(300.0, 150.0)
popoverMenuViewController = troubleViewController!.popoverPresentationController
popoverMenuViewController!.permittedArrowDirections = .Down
popoverMenuViewController!.delegate = self
popoverMenuViewController!.passthroughViews = [self.detailScrollView]
popoverMenuViewController!.sourceView = (notification.object as! UIView).superview
popoverMenuViewController!.sourceRect = CGRect(x: -100, y: 100, width: 300, height: 150)
presentViewController(
troubleViewController!,
animated: true,
completion: nil)
}
any help please?
thanks!
You have to add your popoverMenuViewController in one of the nested subviews of scrollview or in scrollview by
scrollview.addsubview(popoverMenuViewController)
If popoverMenuViewController controller is UIViewController, then add container view in scrollview and link that container view to popoverMenuViewController. later you can add container view as a subview of scrollview.
here, is the link how container view can be implemented. How to use a 'Container View' in iOS?
And
http://spin.atomicobject.com/2015/07/21/ios-container-views/
or add popoverMenuViewController in one of the nested subview of scrollview
subviewofscrollview.addsubview(popoverMenuViewController)
Set your popoverMenuViewController anchor according to the subview content size, where you added this popoverMenuViewController.
I want to create a simple view over keyboard, when users tap "Attach" button in inputAccessoryView.
Something like this:
Is there an easy way to do it? Or i should create my custom keyboard?
You can add that new subview to your application window.
func attach(sender : UIButton)
{
// Calculate and replace the frame according to your keyboard frame
var customView = UIView(frame: CGRect(x: 0, y: self.view.frame.size.height-300, width: self.view.frame.size.width, height: 300))
customView.backgroundColor = UIColor.redColor()
customView.layer.zPosition = CGFloat(MAXFLOAT)
var windowCount = UIApplication.sharedApplication().windows.count
UIApplication.sharedApplication().windows[windowCount-1].addSubview(customView);
}
Swift 4 version:
let customView = UIView(frame: CGRect(x: 0, y: self.view.frame.size.height - 300, width: self.view.frame.size.width, height: 300))
customView.backgroundColor = UIColor.red
customView.layer.zPosition = CGFloat(Float.greatestFiniteMagnitude)
UIApplication.shared.windows.last?.addSubview(customView)
The trick is to add the customView as a top subview to the UIWindow that holds the keyboard - and it happens to be the last window in UIApplication.shared.windows.
Swift 4.0
let customView = UIView(frame: CGRect(x: 0, y: self.view.frame.size.height-300, width: self.view.frame.size.width, height: 300))
customView.backgroundColor = UIColor.red
customView.layer.zPosition = CGFloat(MAXFLOAT)
let windowCount = UIApplication.shared.windows.count
UIApplication.shared.windows[windowCount-1].addSubview(customView)
As Tamás Sengel said, Apple's guidelines does not support adding a view over the keyboard. The recommended way to add a view over keyboard in Swift 4 & 5 is:
1) Add view with your "Next" button in your storyboard as external view and connect in your class (see Explain Image), in my case:
IBOutlet private weak var toolBar: UIView!
2) For the textfield you want to add your custom view over keyboard, add it as accessory view in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
phoneNumberTextField.inputAccessoryView = toolBar
}
3) Add action for "Next" button:
#IBAction func nextButtonPressed(_ sender: Any) {
descriptionTextView.becomeFirstResponder()
// or -> phoneNumberTextField.resignFirstResponder()
}
Explain Image:
Method 2: Result with image
In TableView Controller - add stricked view at bottom
Please follow this great link to handle safe area for screens like iPhone X if you want to use this method(2). Article: InputAccessoryView and iPhone X
override var inputAccessoryView: UIView? {
return toolBar
}
override var canBecomeFirstResponder: Bool {
return true
}
Do you have find some effective method to solve this problem? In iOS9,you put your customView on the top of the windows:
UIApplication.sharedApplication().windows[windowCount-1].addSubview(customView);
But if the keyboard dismisses, the top Windows will be removed, so your customView will be removed.
Looking forward for your help!
Thank you for your help!
You can definitely add the view to your application’s window, and you can also add another window entirely. You can set its frame and level. The level could be UIWindowLevelAlert.
While this can be possible with accessing the topmost window, I would avoid doing this, as it clearly interferes with Apple's guidelines.
What I would do is dismissing the keyboard and replacing its frame with a view with same dimensions.
The keyboard's frame can be accessed from keyboard notifications listed here, their userInfo contain a key that can be accessed with UIKeyboardFrameEndUserInfoKey.