Change navigation title position from left to right
I assume this is what you are looking for. But consider this is applied by iOS automatically dependend on your language settings.
NavigationView {
Color.init("midnight blue")
.edgesIgnoringSafeArea(.vertical)
.navigationBarTitle("המרות מטבע")
.navigationBarItems(trailing:
Button(action: {
print("button pressed")
}) {
Image(systemName: "plus")
.foregroundColor(Color.orange)
})
}.environment(\.layoutDirection, .rightToLeft)
How to change the uinavigationbar title's position?
Create a UIView
object add UIButton
and UILabel
in it to show a similar view. Add this custom view into left bar button item of navigation controller.
An example screen shot to visualise a custom view and related code:
override func viewDidLoad() {
super.viewDidLoad()
var customView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 44.0))
customView.backgroundColor = UIColor.yellow
var button = UIButton.init(type: .custom)
button.setBackgroundImage(UIImage(named: "hamburger"), for: .normal)
button.frame = CGRect(x: 0.0, y: 5.0, width: 32.0, height: 32.0)
button.addTarget(self, action: #selector(menuOpen(button:)), for: .touchUpInside)
customView.addSubview(button)
var marginX = CGFloat(button.frame.origin.x + button.frame.size.width + 5)
var label = UILabel(frame: CGRect(x: marginX, y: 0.0, width: 65.0, height: 44.0))
label.text = "Inbox"
label.textColor = UIColor.white
label.textAlignment = NSTextAlignment.right
label.backgroundColor = UIColor.red
customView.addSubview(label)
var leftButton = UIBarButtonItem(customView: customView)
self.navigationItem.leftBarButtonItem = leftButton
}
func menuOpen(button: UIButton) {
// Handle menu button event here...
}
How do I left align the title of a navigation bar in Xcode?
You can use the navigationItems titleView to add a UILabel with left alignment and then set its frame using auto layout like this:
let label = UILabel()
label.text = "Title Label"
label.textAlignment = .left
self.navigationItem.titleView = label
label.translatesAutoresizingMaskIntoConstraints = false
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: label.superview, attribute: .centerX, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: label.superview, attribute: .width, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: label.superview, attribute: .centerY, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: label.superview, attribute: .height, multiplier: 1, constant: 0))
How to change .navigationBarTitle alignment to trailing for iOS in SwiftUI (or, make it Right To Left)
It is possible to change any view
right-to-left by giving them below modifiers:
.environment(\.layoutDirection, .rightToLeft)
.flipsForRightToLeftLayoutDirection(true)
And it can be used for the navigation bar as well:
NavigationView {
VStack {
//Your views ...
.navigationBarTitle(Text("Title"))
}
}
.environment(\.layoutDirection, .rightToLeft)
.flipsForRightToLeftLayoutDirection(true)
Navigation bar title alignment
// here to create a UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 1;
// set your custom font for title
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"ORDER DETAILS"];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(0,5)];
// set line spacing
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:6];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[string addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [string length])];
label.attributedText = string;
[label sizeToFit];
self.navigationItem.titleView = label;
copied from :Navigationbar title alignment issue
or else the better idea is made the title as an UiImage and u can use that image as Navigation bar title. Use a properly sized 44x44 pt image with 2x and 3x versions.
UIImageView* titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Orderpng.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIView* titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
imageView.frame = titleView.bounds;
[titleView addSubview:imageView];
self.navigationItem.titleView = titleView;
enjoy coding :)
set the alignment of title of navigation bar
You don't have to use UILabel. You can directly use this code:
[self.navigationItem setTitle:@"Your Title"];
and you're good to go! This code will automatically lay its self to the center of the navigation bar.
Or if you actually have an instance of UINavigationController, use:
[self setTitle:@"Your Title"];
Related Topics
Why Can You Assign Non-Optional Values to Optional Types in Swift
Can't Use Concrete Subclass to Implement a Property in Protocol in Swift
Common Equatable Class on Swift
Generic Swift Dictionary Extension for Nil Filtering
Argument for Generic Parameter Could Not Be Inferred
What Is 'Where Self' in Protocol Extension
Can You Give Uistackview Borders
Swift Anyobject Is Not Convertible to String/Int
Nsbundle.Mainbundle().Urlforresource("Bach1", Withextension: "Jpg") Returning Null
How to Move a Rotated Scnnode in Scenekit
Swift: Does Closure Have References to Constants or Variables
Macos/Swift Capture Audio with Avcapturesession
Hstack with Sf Symbols Image Not Aligned Centered
How to Detect That Parameter Is a Tuple of Two Arbitrary Types
How to Get the Realy Fixed Device-Id in Swift
How to Change Colour of the Certain Words in Label - Swift 3