How do you add a navigation bar to a view controller (collection view controller, actually) that is not embedded in a navigation controller? I tried dragging a Navigation Bar onto the view, but it's just not sticking. This is in Swift.
Try putting this code in your viewDidLoad:
let height: CGFloat = 75
let navbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: height))
navbar.backgroundColor = UIColor.whiteColor()
navbar.delegate = self
let navItem = UINavigationItem()
navItem.title = "Title"
navItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .Plain, target: self, action: nil)
navItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .Plain, target: self, action: nil)
navbar.items = [navItem]
view.addSubview(navbar)
collectionView?.frame = CGRect(x: 0, y: height, width: UIScreen.mainScreen().bounds.width, height: (UIScreen.mainScreen().bounds.height - height))
height can, of course, be anything you want. And the actions for the UIBarButtons are selectors to whatever function you want. (You also don't need to have buttons at all).
Edits:
Adjusted the collectionView's frame so it would not overlap with UINavigationBar.
Made height a constant so all its references can be changed in one place.
Here is version for swift 5.
class ViewController: UIViewController, UINavigationBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(toolbar)
toolbar.delegate = self
let height: CGFloat = 75
let navbar = UINavigationBar(frame: CGRect(x: 20, y: 20, width: UIScreen.main.bounds.width, height: height))
navbar.backgroundColor = UIColor.white
navbar.delegate = self
let navItem = UINavigationItem()
navItem.title = "Title"
navItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .plain, target: self, action: nil)
navItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .plain, target: self, action: nil)
navbar.items = [navItem]
view.addSubview(navbar)
self.view.frame = CGRect(x: 0, y: height, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height - height))
}
}
Updated answer for Swift 4:
private func addNavigationBar() {
let height: CGFloat = 75
var statusBarHeight: CGFloat = 0
if #available(iOS 13.0, *) {
statusBarHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
} else {
statusBarHeight = UIApplication.shared.statusBarFrame.height
}
let navbar = UINavigationBar(frame: CGRect(x: 0, y: statusBarHeight, width: UIScreen.main.bounds.width, height: height))
navbar.backgroundColor = UIColor.white
navbar.delegate = self as? UINavigationBarDelegate
let navItem = UINavigationItem()
navItem.title = "Sensor Data"
navItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(dismissViewController))
navbar.items = [navItem]
view.addSubview(navbar)
self.view?.frame = CGRect(x: 0, y: height, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height - height))
}
Related
I tried to make the navigation-bar like this but nothing is going on right. I have added rightbarbutton successfully but in the left side I couldn't add the navigation bar.
I have tried these following codes
private func configureItems(){
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
//self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: nil)
//self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "image8"), style: .done, target: self, action: nil)
let customView = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
customView.text = "John Doe"
customView.textAlignment = .left
customView.layer.masksToBounds = true
navigationItem.leftBarButtonItem = UIBarButtonItem(
customView: customView
)
// let customImageView = UIImageView(image:UIImage(named: "image"))
// customImageView.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
// navigationItem.leftBarButtonItem = UIBarButtonItem(
// customView: customView)
let navController = navigationController!
let image = UIImage(named: "image8")!
let imageView = UIImageView(image: image)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
imageView.frame = CGRect(x: 0, y: 0, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
imageView.center = navController.navigationBar.center
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: customView)
navigationItem.titleView = imageView
I've created my own navigation bar:
let height = 100
let navbar = UINavigationBar()
navbar.frame.size.width = UIScreen.main.bounds.size.width
navbar.frame.size.height = CGFloat(height)
navbar.frame = CGRect(x: 0,
y: 0,
width: navbar.frame.size.width,
height: navbar.frame.size.height)
navbar.barTintColor = Colors.schedulerDarkGray
navbar.titleTextAttributes = [.foregroundColor: Colors.schedulerBlue!]
navbar.tintColor = Colors.schedulerBlue
let navItem = UINavigationItem()
navItem.title = "Add employee"
navItem.rightBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancel))
navbar.items = [navItem]
view.addSubview(navbar)
self.view?.frame = CGRect(x: 0, y: height, width: Int(UIScreen.main.bounds.width), height: (Int(UIScreen.main.bounds.height) - height))
I don't know why I can't set my own height. I can change the x and y position, and width as well.
When I see it in Debug View Hierarchy, it looks like
Do you have any idea why it doesn't work as it should?
I have found solution:
I added this line in view controller ViewDidLoad
self.additionalSafeAreaInsets.top = height
This helped me: https://developer.apple.com/forums/thread/88202#274620
I have added a subview like so:
func showPlayerView2() {
self.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height - 56)
//let theView = myView
let theView = PlayerView()
theView.willMove(toParentViewController: self.navigationController)
self.navigationController?.addChildViewController(theView)
theView.view.isUserInteractionEnabled = true
theView.playPauseButton.isUserInteractionEnabled = true
self.navigationController?.view.bringSubview(toFront: theView.view)
self.navigationController?.view.addSubview(theView.view)
theView.view.frame = CGRect(x: 0, y: self.view.frame.maxY, width: 375, height: 56)
theView.didMove(toParentViewController: self.navigationController)
self.view.layoutSubviews()
}
PlayerView Has a button that is supposed to print something to the screen when pressed.
Even after setting isUserInteractionEnabled to true, it still does not register touches and they pass through to the view below it. I have looked at nearly every SO question relating to this and none of the answers have worked. What can I do to fix this?
Try this code if your looking for a common custom navigation menu
func setupPageNavigationBar(){
navigationController?.navigationBar.barTintColor = UIColor(red:1.00, green:0.22, blue:0.22, alpha:1.0)
navigationController?.navigationBar.translucent = false
let backButton = UIButton(type:.System)
backButton.setImage(UIImage(named: "img_back")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
backButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let imgButton = UIButton(type:.System)
imgButton.setImage(UIImage(named: "img_logo_bar")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
imgButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: backButton),UIBarButtonItem(customView:imgButton)]
backButton.addTarget(self, action: #selector(self.goBack), forControlEvents: .TouchUpInside)
let titleLabel = UILabel()
titleLabel.text = "SOME TITLE"
let titleLabel2 = UILabel()
titleLabel2.text = "Some text"
titleLabel.font = UIFont.boldSystemFontOfSize(15)
titleLabel.textColor = UIColor.whiteColor()
titleLabel.textAlignment = .Left
titleLabel2.font = UIFont.systemFontOfSize(11)
titleLabel2.textColor = UIColor.whiteColor()
titleLabel2.textAlignment = .Left
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: view.frame.width-90, height: 50)
//titleView.backgroundColor = UIColor.whiteColor()
titleView.addSubview(titleLabel)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleView.addSubview(titleLabel2)
titleLabel2.translatesAutoresizingMaskIntoConstraints = false
//titleLabel.frame = CGRect(x: 0, y: 0, width: 134, height: 34)
let viewDict = ["titleLabel":titleLabel,"titleLabel2":titleLabel2]
titleView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[titleLabel]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: viewDict))
titleView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[titleLabel2]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: viewDict))
titleView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-5-[titleLabel(20)]-0-[titleLabel2(15)]", options: NSLayoutFormatOptions(), metrics: nil, views: viewDict))
//5+20+5+15+5
navigationItem.titleView = titleView
let homeButton = UIButton(type:.System)
homeButton.setImage(UIImage(named: "img_home")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
homeButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: homeButton)
homeButton.addTarget(self, action: #selector(self.gotoHome), forControlEvents: .TouchUpInside)
//CGRectMake()
}
And In viewControllers
override func viewDidLayoutSubviews()
{
self.setupPageNavigationBar()
}
I am trying to add two buttons and a SearchBar to my navigation controller but the alignment is not correct. I have tried almost everything such as changing the size of SearchBar, UIButton or changing the Y positioning but it didn't work. Any idea how to fix this issue?
I am using iOS 11 and Xcode 9
searchController = UISearchController(searchResultsController: nil)
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 200, height: 30)
searchController?.delegate = self
searchController?.searchResultsUpdater = self
let refineButton = UIButton.init(type: UIButtonType.custom)
refineButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
refineButton.setImage(#imageLiteral(resourceName: "settings-button"), for: UIControlState.normal)
refineButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
refineButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
let refineItem = UIBarButtonItem(customView: refineButton)
navigationItem.leftBarButtonItem = refineItem
navigationItem.titleView = searchController?.searchBar
searchController?.searchBar.sizeToFit()
Please check :
override func viewWillLayoutSubviews() {
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 300, height: 30)
searchController?.searchBar.sizeToFit()
}
I'm an absolute IOS newbie studying Swift.
I'm trying to create an app with a calendar UI control on top (already got that working), and a listview on the bottom.
Exactly like the picture on the bottom (and I apologise for the huge pic).
Now, like the calendar app, the screen will only show the calendar control, but if you click a button - the listview will reveal itself on the bottom and the screen will become split.
The only way I can think of doing this is to generate the UITableView in code, assigning the current ViewController as both delegate and datasource, and animating both the views (calendar and listview) to grow/shrink to split the screen.
I don't want to use StackView because that's IOS-9 and up.
Is there a better way to do this?
I suspect you're trying to do something like this. Hope it helps:
Swift 2:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRectMake(0, 0, screenWidth, screenWidth)
view1.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRectMake(0, 0, screenWidth, 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.Center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRectMake(0,0,200,100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", forState: .Normal)
btn.addTarget(self, action: "showView2:", forControlEvents: .TouchUpInside)
view1.addSubview(btn)
view2.frame = CGRectMake(0, screenHeight, screenWidth, screenHeight-screenWidth)
view2.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRectMake(0, (screenHeight-screenWidth)/2-25, screenWidth, 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.Center
view2.addSubview(lbl2)
}
var c = 0
func showView2(sender : UIButton) {
UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRectMake(0, self.screenWidth, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", forState: .Normal)
}
else {
self.view2.frame = CGRectMake(0, self.screenHeight, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", forState: .Normal)
}
}, completion: { finished in
})
c++
}
Swift 3:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenWidth)
view1.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", for: .normal)
btn.addTarget(self, action: #selector(showView2(_:)), for: .touchUpInside)
view1.addSubview(btn)
view2.frame = CGRect(x: 0, y: screenHeight, width: screenWidth, height: screenHeight-screenWidth)
view2.backgroundColor = UIColor.red.withAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRect(x: 0, y: (screenHeight-screenWidth)/2-25, width: screenWidth, height: 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.center
view2.addSubview(lbl2)
}
var c = 0
func showView2(_ sender : UIButton) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRect(x: 0, y: self.screenWidth, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", for: .normal)
}
else {
self.view2.frame = CGRect(x: 0, y: self.screenHeight, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", for: .normal)
}
}, completion: { finished in
})
c += 1
}