ios


LeftBarButtonItem position is not correct


I am trying to add a leftbarbuttonitem like this:
with this code:
let button = UIButton.init(type: .custom)
button.setImage(UIImage.init(named: "WhiteBackButton.png"), for: UIControlState.normal)
button.addTarget(self, action:#selector(self.backButtonSelected), for: UIControlEvents.touchUpInside)
button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
let barButton = UIBarButtonItem.init(customView: button)
self.navigationItem.leftBarButtonItem = barButton
I am using this image:
and it sends up looking pushed over like this:
How do I stop it from pushing too far from the left?
WhiteBackButton.png image most likely has white space around/in front of the arrow.
I suggest also be using:
UIBarButtonItem(image:style:target:action:) instead of UIButton it will simplify your code...
https://developer.apple.com/reference/uikit/uibarbuttonitem/1617163-init
If you are changing the barButtonItems a lot can extend ViewController with the following functions and add them to viewWillAppear:
//should have a left and right of each:
func addRightBarButtonWithImage(_ buttonImage: UIImage) {
let rightButton: UIBarButtonItem = UIBarButtonItem(image: buttonImage, style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.toggleRight))
navigationItem.rightBarButtonItem = rightButton
}
func setNavigationBarItem() {
self.addLeftBarButtonWithImage(UIImage(named: “icon.png”)!)
self.addRightBarButtonWithImage(UIImage(named: “icon.png”)!)
}
Hope this helps! Cheers!
let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
negativeSpacer.width = -10
self.navigationItem.leftBarButtonItems = [negativeSpacer, leftBarButton]

Related Links

UIButton in scrollView position unexpected
cross building binutils 2.25.1 for iOS8.4 (arm64-apple-darwin14.0.0)
SWIFT: Modal Segue From TableViewCell (Prototype Cell) Is Not Working
NSDictionary is nil when I decode with NSKeyedUnarchiver even when passing valid data object
Why are my Parse API requests increasing rapidly each day?
How to check valid url?
Automatic Login in IOS Swift [closed]
In UILabel, multiple strings with multiple colors for each string?
Swift lazy load property Alamofire request
Drag and launch type thing in Swift Sprite Kit?
Extend navigation bar under status bar in interface building
UIBezierPath: byRoundingCorners: doesn't work
How To Change The Title Color of an MFMailComposeViewController?
Preventing UIImagePicker memory problems
CoreData. Manage multiple coordinators, in multi thread, multi context
AVPlayer(ViewController): can not play HLS HD video

Categories

HOME
math
amazon-s3
asp.net-identity
common-lisp
selenium
appium-ios
watson
styling
runtime-error
streaming
android-sms
textbox
box
cross-domain
wget
xorg
librsvg
command-line-arguments
tapi
highlight
group-by
deserialization
jsqmessagesviewcontroller
eigen3
amplitude
lexikjwtauthbundle
hp-alm
pydev
cayley
pcap
smallbasic
mathdotnet
point-cloud-library
effects
android-button
android-security
meanjs
nest-thermostat
swipe
subtotal
codeskulptor
advanced-installer
titanium-alloy
uistoryboardsegue
ipod-touch
btle
monogame
maven-jetty-plugin
hydra
powerpc
rating-system
database-cleaner
laravel-routing
master-data-management
jawr
jquery-ui-accordion
angular-directive
vs-2015-preview
nsd
flattr
dashing
insert-update
vlc-android
pager
groups
uploadify
mutation
jquery-load
balana
screen-capture
xelement
ane
document-ready
actioncable
python-dragonfly
python-2.3
spymemcached
android-gpuimageview
heroku-toolbelt
translate3d
maven-cargo
spatial-index
scalariform
pencilblue
llvm-gcc
conditional-statements
linguistics
aps
dojo.gridx
xacml3
grunt-contrib-copy
css3pie
jxta
notifyicon
parsekit
location-client
pligg
git-gui
jflow
adlds
flexpaper
httpcontext.cache
emacs-ecb
openfeint
cluetip
android-searchmanager
apache-config
mkmapviewdelegate
myisam
contracts
geneva-framework
sharepoint-feature
ants

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App