I just try to extend my height of navigation bar...I tried that code below, Its working fine....but it can't able to show the title? I tried self.title ="" and self.navigationitem.title ="" also......
self.navBar.tintColor = UIColor.blue
// self.navigationItem.title = "Instrumental"
self.title = "Instrumental"
navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
let backButton = UIButton(type: .custom)
backButton.frame = CGRect(x: 380, y: 15, width: 30, height: 30)
backButton.setImage(UIImage(named:"vertical-dots (1)"), for: .normal)
// backButton.addTarget(self, action: #selector(ViewController.backButton(_:)), for: .TouchUpInside)
navBar.addSubview(backButton)
func setNavBarToTheView() {
self.navBar.frame = CGRect(x:0, y:0, width:420, height:60) // Here you can set you Width and Height for your navBar
self.navBar.backgroundColor = (UIColor.blue)
self.view.addSubview(navBar)
}
You can add title programmatically for navigation bar like this : -
Swift 3
override func viewDidLoad() {
super.viewDidLoad()
self.title = "First View"
}
It appears that you are manually adding a UINavBar to the view. When you do that you lose all of the automatic features when you put your view controller into a navigation controller.
To set the title of your nav bar you need to also create a UINavigationItem and set it to the nav bar's items property.
Things really would be a lot simpler if you put your view controller in a navigation controller. Then you get all of the default behavior including a standard nav bar without the need to do all the work of adding your own.
Related
The default MapMarker hides information that is under it, for example a city name. If zoomed closed enough the marker doesn't cover the name anymore and the name is displayed. When using a custom annotation view both the city name and the annotation is displayed, it looks a lil messy. Is it possible to get the behaviour from MapMarker when using custom annotations?
MapMarker: https://i.stack.imgur.com/OhQxj.png
Custom: https://i.stack.imgur.com/GwO23.png
Maybe something with clustering or collisions, but seems do be annotations colliding with annotations.
setting the value of .displayPriority to some value seems to do the trick. Don't know which value to set though.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotation = MKAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
annotation.image = UIImage(systemName: "circle.fill")
annotation.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
annotation.displayPriority = .defaultHigh
return annotation
}
I'm presenting a SwiftUI view from UIViewController.
let vc = UIHostingController(rootView: SwiftUIView())
navigationController?.pushViewController(vc, animated: true)
struct SwiftUIView: View {
var body: some View {
VStack {
}
.navigationBarTitle("Title")
}
}
The title appears only after the view has appeared. How to fix?
If you use UINavigationController then you need to set the title on the UIHostingController for it to appear instantly. Otherwise the navigation title will only appear after the SwiftUI view is rendered.
You can set the navigation title of any UIViewController using title
let vc = UIHostingController(rootView: SwiftUIView())
vc.title = "Title"
navigationController?.pushViewController(vc, animated: true)
SwiftUI View Hierarchy
Navigation View --> VStack --> Custom SearchView --> TabView
In the view, with the hierarchy mentioned above, I'm unable to remove navigation bar. It's visibility is successfully changed with the code mentioned shared below.
#available(iOS 15.0, *)
func iOS15UIBarSpecs(){
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
navBarAppearance.backgroundColor = UIColor.red.withAlphaComponent(0.5)
navBarAppearance.shadowImage = UIImage()
navBarAppearance.shadowImage = nil
navBarAppearance.shadowColor = nil
UINavigationBar.appearance().standardAppearance = navBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
}
The presence of this navigation bar is blocking tap gestures on any view coming behind it.
The same view was working fine in iOS 14 with the following line of code.
.navigationBarHidden(true)
Any help would be much appreciated. Thanks!
I am a beginner and I wanted to create a side menu on the right side.
So far I have a UIButton on right top side of viewController, what I want is when I click that button I want to show/hide slide menu with say 3 items…
when I click each item it will go to the different view controller. In my project, i am showing slide menu in only one viewController using AMSlideMenu. Thanks in advance.
Anuj just follow the steps-
Create a SideMenuViewController which is sub class of UIViewController , using storyboard how it will look according to the requirement.
Add this SideMenuViewController and its view as a child view controller in parent view controller by UIButton click.
When you done, remove SideMenuViewController from parent View controller and remove its view from parent view.
Repeat 2 and 3 for all view controllers.
Updated code :
Declare in your view controller -
var sideMenuViewController = SideMenuViewController()
var isMenuOpened:Bool = false
In viewDidLoad
sideMenuViewController = storyboard!.instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController
sideMenuViewController.view.frame = UIScreen.main.bounds
In your button Clicked event -
func openAndCloseMenu(){
if(isMenuOpened){
isMenuOpened = false
sideMenuViewController.willMove(toParentViewController: nil)
sideMenuViewController.view.removeFromSuperview()
sideMenuViewController.removeFromParentViewController()
}
else{
isMenuOpened = true
self.addChildViewController(sideMenuViewController)
self.view.addSubview(sideMenuViewController.view)
sideMenuViewController.didMove(toParentViewController: self)
}
}
For Animation:
let transition = CATransition()
let withDuration = 0.5
transition.duration = withDuration
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
sideMenuViewController.view.layer.add(transition, forKey: kCATransition)
There is no built-in control in iOS for side-menu. However, you could use different open source libraries to achieve your goal.
Have a look at the following libraries:
https://github.com/John-Lluch/SWRevealViewController
The detailed tutorial for this library:
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
http://www.raywenderlich.com/32054/how-to-create-a-slide-out-navigation-like-facebook-and-path
I have designed a UIStackView in a StoryBoard and I have added 2 UIbutton with a height contraint set at 70.
My stackview is set to vertical fill and has some constraints set on the storyboard to fit in the Controller's View.
I have added an outlet from the stackView and from the UIbutton.
I am trying to add a third UIButton programatically in the stack View
#IBOutlet weak var Btn1: UIButton!
#IBOutlet weak var myStackView: UIStackView!
var accounts = [Account]()
override func viewDidLoad() {
super.viewDidLoad()
Btn1.setTitle("Name", for: .normal)
let Btn3 = UIButton(frame: CGRect(x: 0, y: 110, width: 100, height: 50))
Btn3.setTitle("Btn3", for: .normal)
Btn3.heightAnchor.constraint(equalToConstant: 70).isActive = true
myStackView.addArrangedSubview(Btn3)
But Btn3 never appears
Well, assuming that your background color is default-white, the button is there, it's just hard to see ;). In other words: its title color is white.
Try setting some colors:
Btn3.backgroundColor = UIColor.yellow
Btn3.setTitleColor(UIColor.red, for: .normal)
You could also create it as a system button if you care about tintColor:
let Btn3 = UIButton(type: .system)
If you create a button using init(frame:), you get a custom button and the tintColor documentation has this to say:
This property has no default effect for buttons with type custom. For
custom buttons, you must implement any behavior related to tintColor
yourself.
Also, rather than specifying a meaningless frame (layout is managed by the stack view anyway) when initializing the button using init(frame:), I'd suggest using .zero:
let Btn3 = UIButton(frame: .zero)
And a final note regarding Swift naming conventions:
Follow case conventions. Names of types and protocols are
UpperCamelCase. Everything else is lowerCamelCase.
So Btn3 suggests a type, while btn3 or loginButton suggests a variable. See "Follow case conventions" in the Swift API Design Guidelines.