How do I make the border x distance away from image? - ios

I want a border around my image, but I want the border to be a certain distance from the actual image. Right now it creates a border right around the image. How would I do this?
let image = UIImage(named: "WhitePeeyr")!
let imageView = UIImageView(image: image)
let padding = CGFloat(20)
let frame = CGRectMake(0, 0, 100 + 2*padding, 40 + 2*padding)
imageView.frame = CGRectInset(frame, padding, padding)
imageView.frame.origin.y = view.frame.size.height - 542
imageView.frame.origin.x = (self.view.bounds.size.width - imageView.frame.size.width) / 2.0
view.addSubview(imageView)
let borderView = UIView(frame: frame)
borderView.layer.borderWidth = 2.0
borderView.layer.borderColor = UIColor.whiteColor().CGColor
borderView.clipsToBounds = true
view.addSubview(borderView)

You will need two separate views (or layers):
let image = UIImage(named: "image")!
let padding = CGFloat(20)
let frame = CGRectMake(0, 0, image.size.width + 2*padding, image.size.height + 2*padding)
let borderView = UIView(frame: frame)
borderView.layer.borderWidth = 2.0
borderView.layer.borderColor = UIColor.whiteColor().CGColor
borderView.clipsToBounds = true
view.addSubview(borderView)
let imageView = UIImageView(image: image)
imageView.frame = CGRectInset(frame, padding, padding)
view.addSubview(imageView)

Related

iOS Swift: create a mask from a UILabel

Hi I'm trying to use a label as a mask for a particle emitter layer.
My particles emitter is already set up, but I'm having a problem to get a mask from a label, this is my code that doesn't work so well.
func emitter() {
// define emitter layer as centered w 80% of smallest dimension
let image = emitterImage
let origin = CGPoint(x: view.bounds.midX - view.bounds.width / 2, y: view.bounds.midY - view.bounds.height / 2)
let center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
let size = CGSize(width: view.bounds.width, height: view.bounds.height)
let rect = CGRect(origin: origin, size: size)
let emitterLayer = CAEmitterLayer()
emitterLayer.emitterShape = CAEmitterLayerEmitterShape.rectangle
emitterLayer.emitterSize = rect.size
emitterLayer.emitterPosition = center
// define cells
let cell = CAEmitterCell()
cell.birthRate = Float(size.width * size.height / 10)
cell.lifetime = 1
cell.velocity = 10
cell.scale = 0.1
cell.scaleSpeed = -0.1
cell.emissionRange = .pi * 2
cell.contents = image.cgImage
emitterLayer.emitterCells = [cell]
// add the layer
view.layer.addSublayer(emitterLayer)
// mask
let font = UIFont(name: "HelveticaNeue", size: 64)!
var unichars = [UniChar]("Text".utf16)
var glyphs = [CGGlyph](repeating: 0, count: unichars.count)
let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)
if gotGlyphs {
let cgpath = CTFontCreatePathForGlyph(font, glyphs[0], nil)!
let path = UIBezierPath(cgPath: cgpath)
let mask = CAShapeLayer()
mask.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
mask.fillColor = UIColor.clear.cgColor
mask.strokeColor = UIColor.white.cgColor
mask.lineWidth = 10.0
mask.path = path.cgPath
emitterLayer.mask = mask
}
}
Problem 1: I got just the first letter ("T") how could I attach all the characters path in one?
if gotGlyphs {
var paths: [UIBezierPath] = []
for glyph in glyphs {
let cgpath = CTFontCreatePathForGlyph(font, glyph, nil)!
let path = UIBezierPath(cgPath: cgpath)
paths.append(path)
}
In this way I got an array of all chars path, but how can I attach them??
Problem 2: The path is rotated by 180 degrees (why???)
Solved Using CATextLayer to create the mask from a label.
let textLayer = CATextLayer()
textLayer.frame = CGRect(x: 0, y: view.bounds.height / 6 * 1, width: yourLabel.frame.width, height: yourLabel.frame.height)
textLayer.rasterizationScale = UIScreen.main.scale
textLayer.contentsScale = UIScreen.main.scale
textLayer.alignmentMode = CATextLayerAlignmentMode.center
textLayer.fontSize = 30
textLayer.font = yourLabel.font
textLayer.isWrapped = true
textLayer.truncationMode = CATextLayerTruncationMode.end
textLayer.string = yourLabel.text
emitterLayer.mask = textLayer

Centre image in navigation bar

I'm calling a JSQMessageViewController and adding an image as the title but it's not centred due to the offset caused by the Back left-button.
Here's my code for adding the image:
let imageView = UIImageView()
imageView.frame.size.width = 40
imageView.frame.size.height = 40
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "avatar_example")
imageView.image = image
navigationItem.titleView = imageView
Thanks :)
Are you sure that the problem is in JSQMessageViewController? Maybe you just need to use standard sizes from title view (44*44) for alignment.
let imageView = UIImageView(image: UIImage(named: "avatar_example"))
imageView.contentMode = .scaleAspectFit
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
imageView.frame = titleView.bounds
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
You need to actually get the width and height of the UINavigationBar and center the image accordingly. Try this
guard let bar = navigationController.navigationBar else { return }
let bannerWidth = bar.frame.size.width
let bannerHeight = bar.frame.size.height
// centers image vertically & horizontally
let bannerX = bannerWidth / 2 - imageView.frame.width / 2
let bannerY = bannerHeight / 2 - imageView.frame.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = logoImageView

Set frame CGReact x position in middle like centerXAnchor NSLayoutConstraint programmatically

I am creating a simple app which set image as circle by calculate with frame of image.
Here is I declare image variable:
lazy var imageUserDetailProfileView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.layer.borderWidth = 1
imageView.image = UIImage(named: "profile-icon")
imageView.layer.borderColor = UIColor(red:0.00, green:0.50, blue:0.00, alpha:1.0).cgColor
imageView.clipsToBounds = true
return imageView
}()
Here is i made that frame width and height of image became circle
imageUserDetailProfileView.frame = CGRect(x: view.frame.midX, y: 20,width: 100, height: 100)
imageUserDetailProfileView.layer.cornerRadius = imageUserDetailProfileView.frame.height/2
Now i want image should stay in middle of screen like using NSLayoutConstraint which has method centerXAnchor.
How to solve this problem?
let midX = view.frame.size.width / 2
let midY = view.frame.size.height / 2
Try This Code.
Custom Method For Image Set In Center Of Any View. Just Call Method And Pass Image & View. Customize according to Use. Swift 4
func setImageInCenterOfView(image:UIImage,view:UIView) {
let imageWidth:CGFloat = 100
let myImageView = UIImageView.init(frame: CGRect.init(x: (view.frame.size.width/2)-imageWidth/2, y: (view.frame.size.height/2)-imageWidth/2, width: imageWidth, height: imageWidth))
myImageView.clipsToBounds = true
myImageView.layer.cornerRadius = myImageView.frame.size.height/2
myImageView.image = image
view.addSubview(myImageView)
view.layoutIfNeeded()
}

My scrollView cut my last picture Swift 3

I have an horizontal scrollView, where I add my array of image, but it alway cut the picture, my content mode is scaleToFill. I printed the size of my imageView and my scrollView and gave me the same size, so why can't I see my picture filling all my scrollView
My first Image and
My last image of my array
Here is my code
func getFavoriteSalon()
{
arrayOfDataScrollView = [#imageLiteral(resourceName: "large"), #imageLiteral(resourceName: "Kristoff-image-kristoff-36081750-500-266"), #imageLiteral(resourceName: "image-battle-for-dream-island-39780938-500-266")]
mainScrollView.contentSize.width = (mainScrollView.frame.width) * CGFloat(arrayOfDataScrollView.count)
for i in 0..<arrayOfDataScrollView.count
{
let imageView = UIImageView()
let xPosition = self.mainScrollView.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width , height: self.mainScrollView.frame.height)
imageView.image = arrayOfDataScrollView[i]
imageView.contentMode = .scaleToFill
print(self.mainScrollView.frame.width) =>Give me 375
print(self.mainScrollView.frame.height) =>Give me 200
print("Width => \(imageView.frame.width)") =>Give me 375
print("height => \(imageView.frame.height)") =>Give me 200
mainScrollView.addSubview(imageView)
}
}
I found a solution, for me it's a bug xcode, i work with an emulateur of Iphone7+, but I got the value of an Iphone 7 when i print my variables, Be sure to have the preview, and the emulateur looking the same device
Use this and set content size according image
var xPosition:CGFloat = 0
for i in 0..<arrayOfDataScrollView.count{
var frame: CGRect = CGRect(x: 0, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
frame.origin.x = CGFloat(xPosition)
frame.size = mainScrollView.frame.size
let imageView = UIImageView(frame: frame)
imageView.contentMode = .scaleToFill
imageView.frame.size.width = imageWidth
imageView.frame.size.height = imageHeight
imageView.frame.origin.x = xPosition
imageView.frame.origin.y = 0
mainScrollView.addSubview(imageView)
xPosition += imageView.frame.size.width + 5; (5 you can change this value)
mainScrollView.contentSize = CGSize(width: CGFloat(i) * imageView.frame.size.width, height:
mainScrollView.frame.size.height)
}

Navigation bar with UIImage for title

I want to customize my app's look by using a logo image as the navigation bar's title, instead of plain text. When I use this code
let logo = UIImage(named: "logo.png")
self.navigationItem.titleView = logo;
I get the error "UIImage is not convertible to UIView". How can I do this correctly?
Put it inside an UIImageView
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
I use this. It works in iOS 8
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let image = UIImage(named: "YOURIMAGE")
navigationItem.titleView = UIImageView(image: image)
}
And here is an example how you can do it with CGRect.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 38, height: 38))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "YOURIMAGE")
imageView.image = image
navigationItem.titleView = imageView
}
Hope this will help.
For swift 4 and you can adjust imageView size
let logoContainer = UIView(frame: CGRect(x: 0, y: 0, width: 270, height: 30))
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 270, height: 30))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "your_image")
imageView.image = image
logoContainer.addSubview(imageView)
navigationItem.titleView = logoContainer
I tried #Jack's answer above, the logo did appear however the image occupied the whole Navigation Bar. I wanted it to fit.
Swift 4, Xcode 9.2
1.Assign value to navigation controller, UIImage. Adjust size by dividing frame and Image size.
func addNavBarImage() {
let navController = navigationController!
let image = UIImage(named: "logo-signIn6.png") //Your logo url here
let imageView = UIImageView(image: image)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - (image?.size.width)! / 2
let bannerY = bannerHeight / 2 - (image?.size.height)! / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
Add the function right under viewDidLoad()
addNavBarImage()
Note on the image asset. Before uploading, I adjusted the logo with extra margins rather than cropped at the edges.
Final result:
You can use custom UINavigationItem so, you only need to change "Navigation Item" as YourCustomClass on the Main.storyboard.
In Swift 3
class FixedImageNavigationItem: UINavigationItem {
private let fixedImage : UIImage = UIImage(named: "your-header-logo.png")!
private let imageView : UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 37.5))
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
imageView.contentMode = .scaleAspectFit
imageView.image = fixedImage
self.titleView = imageView
}
}
Here is a handy function for Swift 4.2, shows an image with title text:-
override func viewDidLoad() {
super.viewDidLoad()
//Sets the navigation title with text and image
self.navigationItem.titleView = navTitleWithImageAndText(titleText: "Dean Stanley", imageName: "online")
}
func navTitleWithImageAndText(titleText: String, imageName: String) -> UIView {
// Creates a new UIView
let titleView = UIView()
// Creates a new text label
let label = UILabel()
label.text = titleText
label.sizeToFit()
label.center = titleView.center
label.textAlignment = NSTextAlignment.center
// Creates the image view
let image = UIImageView()
image.image = UIImage(named: imageName)
// Maintains the image's aspect ratio:
let imageAspect = image.image!.size.width / image.image!.size.height
// Sets the image frame so that it's immediately before the text:
let imageX = label.frame.origin.x - label.frame.size.height * imageAspect
let imageY = label.frame.origin.y
let imageWidth = label.frame.size.height * imageAspect
let imageHeight = label.frame.size.height
image.frame = CGRect(x: imageX, y: imageY, width: imageWidth, height: imageHeight)
image.contentMode = UIView.ContentMode.scaleAspectFit
// Adds both the label and image view to the titleView
titleView.addSubview(label)
titleView.addSubview(image)
// Sets the titleView frame to fit within the UINavigation Title
titleView.sizeToFit()
return titleView
}
this worked for me in Sept 2015 - Hope this helps someone out there.
// 1
var nav = self.navigationController?.navigationBar
// 2 set the style
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.yellowColor()
// 3
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .ScaleAspectFit
// 4
let image = UIImage(named: "logo.png")
imageView.image = image
// 5
navigationItem.titleView = imageView
I have written this for iOS 10 & iOS 11 and it worked for me:
extension UINavigationBar {
func setupNavigationBar() {
let titleImageWidth = frame.size.width * 0.32
let titleImageHeight = frame.size.height * 0.64
var navigationBarIconimageView = UIImageView()
if #available(iOS 11.0, *) {
navigationBarIconimageView.widthAnchor.constraint(equalToConstant: titleImageWidth).isActive = true
navigationBarIconimageView.heightAnchor.constraint(equalToConstant: titleImageHeight).isActive = true
} else {
navigationBarIconimageView = UIImageView(frame: CGRect(x: 0, y: 0, width: titleImageWidth, height: titleImageHeight))
}
navigationBarIconimageView.contentMode = .scaleAspectFit
navigationBarIconimageView.image = UIImage(named: "image")
topItem?.titleView = navigationBarIconimageView
}
}
Swift 5.1+, Xcode 13+
Sometimes if your image is in high resolution then, imageView shifts from centre, I would suggest using this method
lazy var navigationTitleImageView = UIImageView()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationTitleImageView.image = logo
self.navigationTitleImageView.contentMode = .scaleAspectFit
self.navigationTitleImageView.translatesAutoresizingMaskIntoConstraints = false
if let navC = self.navigationController{
navC.navigationBar.addSubview(self.navigationTitleImageView)
self.navigationTitleImageView.centerXAnchor.constraint(equalTo: navC.navigationBar.centerXAnchor).isActive = true
self.navigationTitleImageView.centerYAnchor.constraint(equalTo: navC.navigationBar.centerYAnchor, constant: 0).isActive = true
self.navigationTitleImageView.widthAnchor.constraint(equalTo: navC.navigationBar.widthAnchor, multiplier: 0.2).isActive = true
self.navigationTitleImageView.heightAnchor.constraint(equalTo: navC.navigationBar.widthAnchor, multiplier: 0.088).isActive = true
}
}
and viewWillDisappear()
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationTitleImageView.removeFromSuperview()
}
or else just reduce the image size
If you'd prefer to use autolayout, and want a permanent fixed image in the navigation bar, that doesn't animate in with each screen, this solution works well:
class CustomTitleNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
let logo = UIImage(named: "MyHeaderImage")
let imageView = UIImageView(image:logo)
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
navigationBar.addSubview(imageView)
navigationBar.addConstraint (navigationBar.leftAnchor.constraint(equalTo: imageView.leftAnchor, constant: 0))
navigationBar.addConstraint (navigationBar.rightAnchor.constraint(equalTo: imageView.rightAnchor, constant: 0))
navigationBar.addConstraint (navigationBar.topAnchor.constraint(equalTo: imageView.topAnchor, constant: 0))
navigationBar.addConstraint (navigationBar.bottomAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 0))
}
Programmatically could be done like this.
private var imageView: UIView {
let bannerWidth = navigationBar.frame.size.width * 0.5 // 0.5 its multiplier to get correct image width
let bannerHeight = navigationBar.frame.size.height
let view = UIView()
view.backgroundColor = .clear
view.frame = CGRect(x: 0, y: 0, width: bannerWidth, height: bannerHeight)
let image = UIImage(named: "your_image_name")
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
view.addSubview(imageView)
return view
}
The just change titleView
navigationItem.titleView = imageView
let's do try and checkout
let image = UIImage(named: "Navbar_bg.png")
navigationItem.titleView = UIImageView(image: image)
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .ScaleAspectFit
let imageView = UIImageView(frame: (CGRect(x: 0, y: 0, width: 40, height:
40)))
imageView.contentMode = .scaleAspectFit
let image = UIImage (named: "logo") // logo is your NPG asset
imageView.image = image
self.navigationItem.titleView = imageView
Works for me in swift 4 (square image 40x40)
let imageView = UIImageView()
imageView.frame.size.width = 40
imageView.frame.size.height = 40
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "YOUR_IMAGE_NAME")
imageView.image = image
navigationItem.titleView = imageView
If you want other measures, try
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 100.5)))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "YOUR_IMAGE_NAME")
imageView.image = image
navigationItem.titleView = imageView
I hope it serves you. It works for me.
Objective-C version:
//create the space for the image
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 256, 144)];
//bind the image with the ImageView allocated
myImage.image = [UIImage imageNamed:#"logo.png"];
//add image into imageview
_myNavigationItem.titleView = myImage;
Just in case someone (like me) had arrived here looking for the answer in Objective-C.
This worked for me... try it
let image : UIImage = UIImage(named: "LogoName")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
imageView.contentMode = .scaleAspectFit
imageView.image = image
navigationItem.titleView = imageView
In order to get the image view with the proper size and in the center, you should use the following approach:
let width = 120 // choose the image width
let height = 20 // choose the image height
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)) //44 is the standard size of the top bar
let imageView = UIImageView(frame: CGRect(x: (view.bounds.width - width)/2, y: (44 - height)/2, width: width, height: height))
imageView.contentMode = .scaleAspectFit //choose other if it makes sense
imageView.image = UIImage(named: "your_image_name")
titleView.addSubview(imageView)
navigationItem.titleView = titleView

Resources