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
This question asked to be implemented in Swift 4, iOS 11
Is there any way to make every subview of ViewController's view to be pushed down when it is under UINavigationBar?
If navigation bar is NOT TRANSLUCENT the subview is under it. This is what I want.
Desired Result
But when navigation bar is TRANSLUCENT the subview is lying under it. I dont want it. I want the subview is pushed down just be like if navigation bar is not translucent.
Undesired Result
I create the view programmatically :
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.red
let navBar = (self.parent as? UINavigationController)?.navigationBar
navBar?.isTranslucent = true
}
func makeChildView() {
let myframe = CGRect(x: 0, y: 10, width: self.view.frame.width, height:
self.view.frame.height * 0.4)
let view = UIView(frame: myframe)
view.backgroundColor = UIColor.green
self.view.addSubview(view)
}
Using Autolayout
I am able to solve this problem using autolayout. But I just want to know how to achieve this result without autolayout if possible. Is there any other approach?
Swift 3.x
navBar?.isTranslucent = true
self.automaticallyAdjustsScrollViewInsets = false
Add this line & you are good to go.
Hey I am trying to add a custom view to a navigation bar. The current result is like so:
I initialise this navigation bar like so:
func setupNavBar() {
navigationController?.navigationBar.isTranslucent = false
let navBarView = ExamCoachNavBarView(frame: CGRect(x: 0, y: 0, width: 236, height: 45))
navigationItem.titleView = navBarView
}
I want to move the custom view down to the bottom of the nav bar. I have tried changing the y value for the navBarView frame. This make absolutely no difference those. Regardless to what that value is the frame always stays in the same position at the top of the bar. What am I doing wrong here? How can I move the bar to the bottom of the screen?
I have created my own subclass of UIPresentationController and I am presenting a navigation controller using it. The purpose is to somewhat mimic the behavior of UIPopoverPresentationController but allow for more customization.
So the problem I am experiencing is that on iPad when the user adjusts the size of the app using splitview, the navigation bar's height doesn't update correctly.
When the view is in a popover style it is supposed to use a height of 44 for the nav bar and when it is in fullscreen style it uses a height of 64. This is happening correctly upon first presenting the controller. However if the user adjusts the app using splitview the nav bar height does not update at all.
In my UIPresentationController subclass I am doing the following:
I set the frame based on the container view's width:
override func frameOfPresentedViewInContainerView() -> CGRect {
if let containerView = containerView {
if containerView.bounds.width > 500 {
let preferredSize = presentedViewController.preferredContentSize
return CGRect(x: containerView.bounds.width - preferredSize.width - 20, y: 16, width: preferredSize.width, height: preferredSize.height)
} else {
return containerView.bounds
}
} else {
return CGRectZero
}
}
Then I update the frame whenever I get the willLayoutSubviews call:
override func containerViewWillLayoutSubviews() {
presentedViewController.view.frame = frameOfPresentedViewInContainerView()
}
When I examine the presentedViewController's view, it is getting all the correct values and visually is the right size. The only problem is that the nav bar will remain the height that it was originally presented at (whether that is 44 or 64) and will either leave a gap or extend passed its bounds.
It appears I found a solution that works. Inside my containerViewWillLayoutSubviews function I simply access the navigation controller's navigationBar property and manually set its frame correctly.
if let navigationController = presentedViewController as? UINavigationController {
navigationController.navigationBar.frame = CGRect(x: 0,
y: 0,
width: presentedViewController.view.frame.size.width,
height: containerView.bounds.width > 500 ? 44 : 64)
}
This does seem a bit fragile though, but its working fine for me.
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.