Adding UIImage to new Scroll View - swift

I'm trying to create a simple app in Xcode that lets me press a button and it adds an image to a view. I originally had everything working until I decided to add a scroll view, as I wanted to create multiple buttons. My problem is, the created image follows down when scrolling. Im sure this is an easy fix. My code looks like this for each button:
#IBAction func btnAddColorfulLights1(_ sender: Any) {
let image: UIImage = UIImage(named: "ColorfulLights.png")!
let imageView = UIImageView(image: image)
self.view.addSubview(imageView)
imageView.tag = 10
imageView.frame = CGRect(x: 67, y: 39, width: 240, height: 338)
}
I think my problem is in,
self.view.addSubview(imageView)
Where I'm adding the image to view, where I need to be adding it to the scroll view.
This is my control viewer organization/set up:
Here's what it should look like:
Here's the problem:
I know this is probably very easy but I can't seem to find any answers to my specific problem or anything that can help me. Thank you so much!

You need to add the UIImageView containing the image of your lights to the UIView that is the contentView of the UIScrollView.
An easy way to do this is to create an #IBOutlet for the UIView in your scrollView. control-drag from the View under the ScrollView in the Document Outline to your ViewController code. Give the #IBOutlet a name such as:
#IBOutlet weak var scrollViewsView: UIView!
Then, use it to add your adornments:
#IBAction func btnAddColorfulLights1(_ sender: Any) {
let image = UIImage(named: "ColorfulLights.png")!
let imageView = UIImageView(image: image)
self.scrollViewsView.addSubview(imageView)
imageView.tag = 10
imageView.frame = CGRect(x: 67, y: 39, width: 240, height: 338)
}

Related

How add Size and Position to ImageView in UITableView Cell?

I added a image to my UITableView Cell:
cell.imageView!.image = UIImage(named: "Default.png")
Everything works, the image is displayed. Now I want to give a custom size and position for the image, so I try it with this code:
cell.imageView!.frame = CGRect(x: 29, y: 17, width: 14, height: 13)
But for any reason it doesn't work. Any advices?
If your position will not change You can create frame in your TableViewCell
class TableViewCell: UITableViewCell {
#IBOutlet weak var imageView: UIImageView!
override func layoutSubviews() {
super.layoutSubviews()
self.imageView?.frame = CGRect(x: 12, y: 23, width: 12, height: 100)
}
}
You can change frame in tableview(cellForRowAt) but If frame will not change its a bad because cellForRowAt run when scrolling up - down or loading cell . You can define in UITableViewCell once.
Add a custom class for this custom tableViewCell.
Create a custom tableViewCell inside the tableView through the storyboard.
Add image inside this custom tableViewCell.
Add constraints (top, bottom, leading, trailing) to this image.
This way, you won't have to do anything programmatically and the image size issues will be taken care of by these constraints.
It'd be better if you do that frame related things in storyboard or Xib files itself. if you want them with static frame.
RunTime:
You can use NSLayoutConstraints
You can change the frame in draw(_ rect: CGRect) method.

Set UIImageView as background - Swift 4

I want to set a background image behind everything that already exists in the view.
I tried :
override func viewDidLoad() {
super.viewDidLoad()
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "bg-img")
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
}
But it didn't work (it didn't even add the img). So I imported the ImageView component and added the image using the Xcode GUI and it did add the image but is on top of everything. I have tried several Stack Overflow answers (that's where I get the above code from), but I didn't have any luck.
My folder structure looks like this:
My image is inside the Assets.xcassets folder.
UPDATE:
I restarted Xcode and my code worked.
Setting the subviews' frames in viewDidLoad is not a good practice because subviews aren't yet correctly placed at that point. You should set the frames of subviews in viewDidLayoutSubviews, which is called after the self.view and it's subviews are correctly laid out.
Your ViewController:
var backgroundImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.backgroundImage = UIImageView(image: UIImage(named: "bg-img"))
self.backgroundImage.contentMode = .scaleAspectFill
self.view.insertSubview(self.backgroundImage, at: 0)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.backgroundImage.frame = self.view.bounds
}
All you have to do is give the full name of the jpg image:
backgroundImage.image = UIImage(named: "bg-img.jpg")
If it were placed in Assets.xcassets, or a png file, then specifying the extension would have been optional.
Sometimes all you need is to Clean your Build Folder ⇧⌘K, or just restart Xcode.

Swift 4 upgrade issue with imageViews as bar buttons within navigation bar

So I have upgraded to swift 4 and now my left & right uiimageviews set as left/right nav button items are showing as large icons. I have figured out that the Frame setting is not being applied and I am not sure why.
Does anyone know what may cause this?
Here is some code
lazy var leftBarPic: UIImageView = {
let pic = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
pic.clipsToBounds = true
pic.contentMode = .scaleAspectFit
pic.image = myImage.withRenderingMode(.alwaysOriginal)
pic.isUserInteractionEnabled = true
pic.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(postNewsAction)))
pic.backgroundColor = .green
return pic
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBarPic)
}
OK I found a fix. For anyone having similar, what I did was add the property (in this instance the left navigator imageView) to the view's subview. Then added the constraints for width and height. It seems that with the upgrade setting the frame for nav items (image views only) inside the property does not work.

Custom views in Horizontal scroll view. Is it possible?

I am designing an app which has a screen in which I have a horizontal scroll view which I fill with UIViews dynamically depending upon the number of data I have in my array . I did the same via programmatically. I have mentioned my approach below.
1) I put a Scroll view for scrolling horizontally and created a reference for that in my class.
2) I programatically added views as per my code -
var imagevieww = UIImageView()
#IBOutlet weak var hrzntlscrl: UIView!
#IBOutlet weak var scrollview: UIScrollView!
override func viewDidLoad()
{
super.viewDidLoad()
let viewcount = 15
for var i = 0; i < viewcount; i++
{
let viewnew = UIView(frame: CGRectMake( hrzntlscrl.frame.origin.x+110*CGFloat(i), 0, 100.0, hrzntlscrl.frame.height))
viewnew.backgroundColor = UIColor.orangeColor()
imagevieww = UIImageView(frame: CGRectMake(0, 10, 100.0, 50))
imagevieww.backgroundColor = UIColor.blackColor()
viewnew.addSubview(imagevieww)
scrollview.addSubview(viewnew)
}
}
So I just wanted to know that instead of creating a view and the corresponding subviews eg. here imageview and setting their location and frame size programatically , Can I have a standard custom view designed in my IB and use any reference of that in my for loop instead of creating one programmatically? If we can do that,can you please give me some steps.
Yes. This is possible. You can instantiate a class from a nib with
let customView: CustomView = NSBundle.mainBundle().loadNibNamed("CustomViewNibName", owner: self, options: nil)[safe: 0] as? CustomView
You would also need to set the content size of the horizontal scroll view to the combined width of all the views.
But I think that your use case would be better served by using a UICollectionView instad of a scroll view, UICollectionView does all this and more in a much simpler implementation.

setting titleView of custom UINavigationBar

I have a custom navigation bar that doesn't really do any navigation controlling (app only has one view) its instead used as a header to display the logo against a solid background colour. So I put a navigation bar onto the main view controller and created an outlet so I can reference it in the code. (Using swift by the way).
Right now I'm struggling with setting the logo image in the center of the nav bar. I need to do this with the titleView (instead of setting an entire background image). So far what I've read says to use self.navigationItem.titleView in the view controller, but I think that only works when using a UINavigationController. Here is my code so far:
#IBOutlet weak var navBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
let logoImage = UIImageView(frame: CGRect(x:0, y:0, width: 200, height: 45))
logoImage.contentMode = .ScaleAspectFit
let logo = UIImage(named: "logo.png")
logoImage.image = logo
self.navigationItem.titleView = logoImage
}
But this doesn't work. How do I get access to the titleView using a custom UINavigationBar like this?
You need to access UINavigationBar's topItem attribute.
Then you can set the topItem's titleView with a UIImageView.
So in your case:
self.navBar.topItem?.titleView = logoImage