I have a problem with backgroundView property. I tried to add an imageView to background like this
let backgroundImage = UIImage(named: "Fridge_background")!
let backgroundView = UIImageView(image: backgroundImage)
collectionView?.backgroundView = backgroundView
but the problem is that it did not even add this view to view hierarchy. I have already read this article, but this solution did not help me either. What it can be and how did I can fix it? Funny think is that in tableView similar property work just fine as it must.
Broken it is indeed.
I was researching improper positioning of uicollectionview background view
and run into this:
https://blog.spacemanlabs.com/2013/11/uicollectionviews-backgroundview-property-is-horribly-broken/
describes your issue as well.
This seemed to work well in the UIColllectionViewController's viewDidAppear method. All behaviors matched expectations.
let myView = UIView(frame: self.view.frame)
let image = UIImage(named: "image name")
let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: image!.size))
imageView.image = image
myView.addSubview(imageView)
self.collectionView.backgroundView = myView
Related
I tried to set image into UIView but I can't resize it in my view I got something like this:
UIview
let imageName = "rectanguloAzul"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.contentMode = .scaleAspectFill
self.view.addSubview(imageView)
Welcome.
You need to either give the imageView a frame or use Autolayout to set a layout for the image inside the UIView.
so either add this at the end:
imageView.frame = view.frame
But this will not be dynamic and you should learn how to keep updating the frame whenever the superview's frame changes.
Or you can add this, instead:
imageView.translatesAutoresizingMaskIntoConstraints = false
let constraints = [
imageView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
imageView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor)
]
NSLayoutConstraint.activate(constraints)
Anyway, I really recommend you read up about AutoLayout a bit before you continue:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/index.html
https://www.raywenderlich.com/811496-auto-layout-tutorial-in-ios-getting-started
If you find programmatic AutoLayout to be too challenging, I would recommend possibly starting with Storyboards first.
This is the code I'm using to set an image in the title of the nativation bar,
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "fullLogo")
imageView.image = image
self.navigationItem.titleView = imageView;
The issue is that image appears a little too big. How do I resize the image in code? I tried many combinations of the width and height but it doesn't seem to work.
According to documentation:
Custom title views are centered on the navigation bar and may be resized to fit.
That's what happening with your image view. The solution can be to add imageView as subview to UIView and assign UIView to titleView property.
The easier solution may be to set imageView.contentMode to .center but in this case you need to be sure that your fullLogo image is actually 100x30, if it is bigger than titleView frame calculated by iOS your image will overlay navigation bar and view controller's view.
And you can init your UIImageView or container UIView with frame: .zero because it's frame will be recalculated in runtime.
Set
imageView.clipsToBounds = true
For resizing the image this SO Post would help. Hope this helps.
Happy Coding.
I have used scrollview. Over the Scrollview I have a UIView where I can set up my image as a background. But problem is that image height is not merged with UIView height. It is shown when scrolling bottom. I want add image with whole UIView.
Here is my image screen shot
Here is my code
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.image = UIImage(named: "background_image")
backgroundImage.contentMode = .scaleToFill
self.my_view_2.insertSubview(backgroundImage, at:0)
Please Help me
Thanks
Height of your UIImageView is same as screen size, if your view is greater than screen size, then white view will remain at bottom.
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
Update frame of backgroundImage with scroll's view, as:
let backgroundImage = UIImageView(frame: self.my_view_2.bounds)
backgroundImage.autoresizingMask = [.flexibleHeight, .flexibleWidth] // In case if your my_view_2 frames increases
self.my_view_2.insertSubview(backgroundImage, at: 0)
Add this code in your viewDidLoad(). May be helps you to solve.
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
} else {
automaticallyAdjustsScrollViewInsets = false
}
try this
let backgroundImage = UIImageView(frame: my_view_2.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.image = UIImage(named: "background_image")
backgroundImage.contentMode = .scaleToFill
self.my_view_2.addSubview(backgroundImage)
Hope it may help
self.my_view_2.layer.contents = #imageLiteral(resourceName: "background_image");
In storyBoard, I added separate UIView as First responder in view controller.Then I added that view as subview of self.view. But I want self.view should be blurred when subview is shown. How can i do this?
I tried by applying
let imageView = UIImageView(image: UIImage(named: "blur2.jpg"))
imageView.frame = self.view.bounds
imageView.contentMode = .scaleAspectFit
self.view.addSubview(imageView)
let blurEffect = UIBlurEffect(style: .regular)
let blurredEffectView = UIVisualEffectView(effect: blurEffect)
blurredEffectView.frame = imageView.bounds
self.view.addSubview(blurredEffectView)
self.view.addSubview(view_Message)
view_Message.center = self.view.center
view_Message.alpha = 1
view_Message.layer.cornerRadius = 0.3
view_Message.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
view_Message is UIView which is taken as FirstResponder in storyBoard.
But my self.view content goes invisible. I want My self.view should get blur(but should be shown lightly) at the same time I want my subview is shown.
i suggest you to
1. Take a UIView(let say myView) of frame self.view.bounds with clear as backgroundcolor
2. Add imageView into it of same frame
3. set backgroundcolor for imageView white,alpha nearly 0.7,and blur effect.
4. Now add your view_Message into myView as SubView.
5. Atlast add myView to self.view
or you can go by using UIVisualEffectBlur class either in storyboard or programmatically available for iOS 8+.
You can also use visualEffectView rather than blurEffect like this:
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = view.bounds
visualEffectView.alpha = 0.7
self.view.addSubview(visualEffectView)
You can do like this
let frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height )
blurview = UIView.init(frame: frame)
blurview.backgroundColor = UIColor.blackColor()
blurview.layer.opacity = 0.5
self.view.addSubview(blurview)
Check 3D view. It seems That your ImageView is On Top Of all views. This will hide all your views behind it.
Try
self.view.sendSubviewToBack(imageView)
at the end, This will send imagview at back an your all subviews will be visible.
I have tried the code below and the answer to multiple other questions on Stackoverflow and I still cannot get this to work.. I know the image Profile exists in my assets folder. I have made outlets from my navigation bar and navigation item and tried using that but still got nothing. At most the text I have on the title will disappear...I feel as though I am missing something all together.
Located in ViewController's ViewDidLoad function
let image = UIImage(named: "Profile")
let imageView = UIImageView(image: image)
self.navigationItem.titleView = imageView
self.navigationItem.titleView?.sizeToFit()
Thanks for your help!
initially set the frame for imageview
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .ScaleAspectFit
if let image = UIImage(named: "Profile")
{
imageView.image = image
}else
{
imageView.image = UIImage(named: "Profile.png")
}
self.navigationItem.titleView = imageView
In Objective C:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];
where "image" is the instance of the image that you want to set as title view.