Ios 如何设置插入导航栏的图像的位置和大小?
事实上,我不知道如何在导航栏中创建这样的图像,无论是使用导航控制器还是自己使用自定义视图 我需要插入这两个图像作为酒吧按钮和图像标题 因此,我尝试使用导航控制器并在Ios 如何设置插入导航栏的图像的位置和大小?,ios,uinavigationcontroller,Ios,Uinavigationcontroller,事实上,我不知道如何在导航栏中创建这样的图像,无论是使用导航控制器还是自己使用自定义视图 我需要插入这两个图像作为酒吧按钮和图像标题 因此,我尝试使用导航控制器并在viewDidLoad中插入图像,如下所示: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // insert image
viewDidLoad
中插入图像,如下所示:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// insert image title
let aspectRatio : CGFloat = 0.25
let widthOfImageHeader = view.frame.width * 0.5
let heightOfImageHeader = widthOfImageHeader * aspectRatio
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: widthOfImageHeader, height: heightOfImageHeader))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "testImage2")
imageView.image = image
navigationItem.titleView = imageView
// set bar button image
//create a new button
let button = UIButton(type: .custom)
//set image for button
button.setImage(UIImage(named: "hamburgerIcon"), for: .normal)
//set frame
button.frame = CGRect(x: 0, y: 0, width: 53, height: 51)
let barButton = UIBarButtonItem(customView: button)
//assign button to navigationbar
self.navigationItem.leftBarButtonItem = barButton
}
}
但结果如下图所示:
iphone5s的图片标题位置并不完全在中间,右边的条形按钮似乎有点不对劲
如果我指定图像右栏按钮,使用
/
/create a left button
let button = UIButton(type: .custom)
//set image for button
button.setImage(UIImage(named: "hamburgerIcon"), for: .normal)
//set frame
button.frame = CGRect(x: 0, y: 0, width: 53, height: 51)
let barButton = UIBarButtonItem(customView: button)
//assign button to navigationbar
self.navigationItem.leftBarButtonItem = barButton
//create a right button
let button2 = UIButton(type: .custom)
//set image for button
button2.setImage(UIImage(named: "backButton"), for: .normal)
//set frame
button2.frame = CGRect(x: 0, y: 0, width: 53, height: 51)
let barRightButton = UIBarButtonItem(customView: button)
//assign button to navigationbar
self.navigationItem.rightBarButtonItem = barRightButton
结果更为奇怪
其中一个按钮消失了,汉堡包图标应该在左边,现在在右边
如果我使用自定义视图会容易得多,但是…如果我使用像导航栏一样使用的自定义视图,会很奇怪吗?我是iOS开发者的新手。提前感谢好的,下面是我在有此类需求的项目中通常做的事情(导航栏标题处的图像和自定义栏按钮项) 首先回答你的问题,你可以做任何你想做的事
UINavigationController
中时,确实具有自定义视图。但一定要隐藏导航栏UINavigationController
中嵌入可见的导航栏和ViewController导入GPKit
导入UIKit
类ViewController:UIViewController{
//标记:-属性
内部惰性变量按钮\u关闭:UIButton={
let button=ui按钮(类型:。自定义)
setImage(UIImage(名为:“ham”),用于:。正常)
button.imageEdgeInsets=UIEdgeInsets.init(顶部:0,左侧:-30,底部:0,右侧:0)
//addTarget(self,action:#选择器(hamburger(:)),for:.touchUpInside)
返回按钮
}()
//马克:-功能
//标记:覆盖
重写func viewDidLoad(){
super.viewDidLoad()
GPLog(classSender:self,log:“viewDidLoad!”)
//标题视图测试:-
让navBarImage=UIImage(名为:“ic_logo_navbar”)!
self.setNavBarTitleWithFeedback(图像:navBarImage,NavBarIntColor:.白色)
self.makeNavBarColor(颜色:UIColor.colorWithRGBHex(0x332F39),itemsTintColor:.white)
//巴布顿
让barButton=UIBarButtonItem(自定义视图:self.button_关闭)
self.button_Close.frame=CGRect(x:0,y:0,宽度:55.0,高度:44.0)
设negativeSpacer=UIBarButtonItem(barButtonSystemItem:UIBarButtonItem.SystemItem.fixedSpace,目标:nil,操作:nil)
如果可用(iOS 11.0,*){
负片Acer.width=-30
}
self.navigationItem.leftBarButtonItems=[negativeSpacer,barButton]
}
}
扩展视图控制器:GPTitleViewDeleteGate{
func-gpTitleView(userDidTapTitleView-gpTitleView:gpTitleView){
GPLog(classSender:self,log:“userdidtaptitleview”您想在导航栏的标题处放置的确切图像是什么?是“info kehadiran”吗"在上面的图像中?是的,实际上我只是尝试使用不同的图像。但是我希望结果是一样的,因为我指定了宽度和高度。但是位置不完全在中心。好的,但是你也可以分享你想在导航栏中放什么图像吗?当然,你不能放任何大小的图像。谢谢Glenn,I u用图片更新问题检查我下面的答案:D