Can't set titleView in the center of navigation bar because back button
UINavigationBar
automatically centers its titleView
as long as there is enough room. If the title isn't centered that means that the title view is too wide to be centered, and if you set the backgroundColor if your UIImageView
you'll see that's exactly what is happening.
The title view is too wide because that navigation bar will automatically resize the title to hold its content, using -sizeThatFits:
. This means that your title view will always be resized to the size of your image.
Two possible fixes:
The image you're using is way too big. Use a properly sized 44x44 pt image with 2x and 3x versions.
Wrap UIImageView inside of a regular UIView to avoid resizing.
Example:
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.jpeg"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIView* titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
imageView.frame = titleView.bounds;
[titleView addSubview:imageView];
self.navigationItem.titleView = titleView;
Can't set titleView in Navigation bar center and scope of back button increased
Back button is picking its size according to the title of previous viewController. you can change it to a empty String for example, in your previous viewController from where you are pushing write this code.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
How to change the size of titleView in navigation bar. Because there's a gap between titleView and backButton in navigationBar
override func viewDidLoad() {
super.viewDidLoad()
let container = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 22))
let searchBar = UISearchBar()
searchBar.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(searchBar)
let leftButtonWidth: CGFloat = 35 // left padding
let rightButtonWidth: CGFloat = 75 // right padding
let width = view.frame.width - leftButtonWidth - rightButtonWidth
let offset = (rightButtonWidth - leftButtonWidth) / 2
NSLayoutConstraint.activate([
searchBar.topAnchor.constraint(equalTo: container.topAnchor),
searchBar.bottomAnchor.constraint(equalTo: container.bottomAnchor),
searchBar.centerXAnchor.constraint(equalTo: container.centerXAnchor, constant: -offset),
searchBar.widthAnchor.constraint(equalToConstant: width)
])
self.navigationItem.titleView = container
}
iOS Navgation bar title not in center?
If you using default Back Button on Navigation bar,add my working code to previous viewController while pushing to nextViewController
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
And then add title to navigation bar on nextViewController viewDidLoad:
method
[self.navigationItem setTitle:NSLocalizedString(@"Restaurants", nil)];
Handle back button action event from nextViewController like this,
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"G_Back", nil) style:UIBarButtonItemStylePlain target:nil action:@selector(actionToBackBtnFromDetail:)];
Can't centre title text in a navigation bar
This may happen because there may be some other view like leftBarButton, backButton or rightBarButton which causing this issue. Actually you are creating label of width 320.0f which is the complete width of navigation bar
. You need to deduct the size of other views of navigation bar from you label width and it will work for you.
UINavigationItem centering the title
You can't do what you want directly -- the position of your title view is out of your control (when managed by UINavigationBar
).
However, there are at least two strategies to get the effect you want:
1) Add the title view not as the 'proper' title view of the nav bar, but as a subview of the UINavigationBar
. (Note: this is not 'officially' sanctioned, but I've seen it done, and work. Obviously you have to watch out for your title label overwriting bits of the buttons, and handle different size nav bars for different orientations, etc. -- a bit fiddly.)
2) Make an intelligent UIView subclass that displays a given subview (which would be your UILabel) at a position calculated to effectively show the subview perfectly centered on the screen. In order to do this, your intelligent UIView subclass would respond to layout events (or frame
property changes etc.) by changing the position (frame
) of the label subview.
Personally, I like the idea of approach 2) the best.
How to set UIViewController to be shown on back button of Navigation bar?
You can manipulate the viewControllers
property of UINavigationController
object to remove a particular object from stack. So when you are taking the route of ViewController -> LoginViewController-> CartViewController
, then you should remove the LoginViewController
object from the stack. That way when you pop from CartViewController
you will be back to ViewController
.
Check This Question for how to manipulate navigation controller stack
Custom behavior when back button in nav bar pressed
The following are the possibilities for a View Controller to disappear.
- Clicking on a button (other than backBarButtonItem).
- Clicking on the backBarButtonItem
Either case viewWillDisappear:
method will be called. Keep a boolean flag. Lets name it isAnyButtonClicked. And set isAnyButtonClicked = YES
whenever any button is clicked. And override the viewWillDisappear:
method, and play the sound if no button is clicked(ie., isAnyButtonClicked == NO
). Probably your viewWillDisappear:
would look like,
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (!isAnyButtonClicked) {
// Play sound
isAnyButtonClicked = NO;
}
}
Related Topics
Change Table to Edit Mode and Delete Rows Inside a Normal Viewcontroller
Bridge Google Drive API to Swift
Swift 3.0, Alamofire 4.0 Extra Argument 'Method' in Call
How to Use Multi-Path Update with the Rest API in Firebase? Error 400
@State Var Not Updated as Expected in Lazyvgrid
Cannot Assign Value of Type '() -> Void' to Type '(() -> Void)!'
Why My Nsdateformatter Returns Null
iOS Simulator Display Scaled Incorrectly
iOS Bar Item Image Displaying Wrong Color
How to Delete Object from Core Data in Swift 3
How to Return Value After the Execution of the Block? Swift
Xcode 8, iOS 10 - "Starting Webfilter Logging for Process"
How to Open Location Services Screen from Setting Screen
iOS 9 - "Attempt to Delete and Reload the Same Index Path"
iOS - Uiimagewritetosavedphotosalbum
Nsuserdefaults Unreliable in iOS 8
iOS App Deployment Without Appstore
How to Show Complete List When Keyboard Is Showing Up in Swiftui