导航栏的标题将是图像和文本的组合.
>这应该按照最佳做法进行吗?
>怎么做?
var image = UIImage(named: "logo.png")self.navigationItem.TitleVIEw = UIImageVIEw(image: image)
但是,如果必须单独添加文本和图像(例如,在本地化的情况下),您可以将导航栏的标题视图设置为包含图像和文本,方法是将它们添加到UIVIEw并将navigationItem的标题视图设置为例如UIVIEw(假设导航栏是导航控制器的一部分):
// Only execute the code if there's a navigation controller if self.navigationController == nil { return}// Create a navVIEw to add to the navigation barlet navVIEw = UIVIEw()// Create the labellet label = UILabel()label.text = "Text"label.sizetoFit()label.center = navVIEw.centerlabel.textAlignment = NSTextAlignment.Center// Create the image vIEwlet image = UIImageVIEw()image.image = UIImage(named: "Image.png")// To maintain the image's aspect ratio:let imageAspect = image.image!.size.wIDth/image.image!.size.height// Setting the image frame so that it's immediately before the text:image.frame = CGRect(x: label.frame.origin.x-label.frame.size.height*imageAspect,y: label.frame.origin.y,wIDth: label.frame.size.height*imageAspect,height: label.frame.size.height)image.contentMode = UIVIEwContentMode.ScaleAspectFit// Add both the label and image vIEw to the navVIEwnavVIEw.addSubvIEw(label)navVIEw.addSubvIEw(image)// Set the navigation bar's navigation item's TitleVIEw to the navVIEwself.navigationItem.TitleVIEw = navVIEw// Set the navVIEw's frame to fit within the TitleVIEwnavVIEw.sizetoFit()总结
以上是内存溢出为你收集整理的swift – iOS – 在导航栏的标题中添加图像和文本全部内容,希望文章能够帮你解决swift – iOS – 在导航栏的标题中添加图像和文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)