how to add UILabel and UIText on the background image - ios

This is part of my code.
I want to show two UILabel and two UITextField on the background image: backgroundLogin.jpg .
import Foundation
import UIKit
class LoginViewController:UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
let BackGroundImage:UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width , self.view.frame.height))
let image: UIImage = UIImage(named: "backgroundLogin.jpg")!
BackGroundImage.image = image
self.view.addSubview(BackGroundImage)
//self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundLogin.jpg")!)
username.text = "User name"
password.text = "Password"
}
#IBOutlet weak var username: UILabel!
#IBOutlet weak var password: UILabel!
}
But when this code runs, it looks like:
I hope it can have two label on the image, but how to add that ?

Well like I stated in the comment you should try bring the UILabels to the front.
Since you are doing this programatically it can be done with bringSubviewToFront here is Apple's documentation on this and other UIView functionality you can use. Note: UILabel inherits from UIView so you can use these methods.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/doc/uid/TP40006816-CH3-SW46
The code:
override func viewDidLoad() {
super.viewDidLoad()
let BackGroundImage:UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width , self.view.frame.height))
let image: UIImage = UIImage(named: "backgroundLogin.jpg")!
BackGroundImage.image = image
self.view.addSubview(BackGroundImage)
//self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundLogin.jpg")!)
username.text = "User name"
password.text = "Password"
self.view.bringSubviewToFront(username)
self.view.bringSubviewToFront(password)
}

It seems you are adding the UIImageView in code and the UILabels on storyboard, then when you run the app the labels are behind the UIImageView and you can't see then.
Just check it adding BackGroundImage.hidden = true in viewDidLoad.
You can't add UILabels into a UIImageView. One solution would be add all views in code, 1st the image view and then the labels over it. Other option would be add all then in storyboard in the same order.
Like this:

You’re doing a lot here programmatically that you don’t need to do.
In Storyboard, add a UIImageView to a blank ViewController (you
can rename the view controller later if you wish)
With the new UIImageView selected, control-drag to create an outlet in your view controller named backgroundImage
With the UIImageView selected, choose the image you want it to display by going to the Attribute Inspector and from the dropdown
box, select your image. All images in your project should appear in
the dropdown box.
Now add a UILabel to the ViewController and control-drag to create an outlet in the corresponding code
In the viewDidLoad() method, bring the outlet to the front
All this requires code-wise is:
class ViewController: UIViewController {
#IBOutlet weak var backgroundImage: UIImageView!
#IBOutlet weak var username: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.view.bringSubviewToFront(username)
}
}

Related

Unable to show UILabel after add it to subview

#IBOutlet weak var result: UILabel!
#IBOutlet weak var testview: UIView!
override func viewDidLoad() {
super.viewDidLoad()
testview.layer.shadowColor = UIColor.systemGray.cgColor
testview.layer.shadowOpacity = 0.6
testview.layer.shadowOffset = .zero
testview.layer.shadowRadius = 5
testview.layer.shadowPath = UIBezierPath(rect: testview.bounds).cgPath
testview.layer.shouldRasterize = true
testview.layer.rasterizationScale = UIScreen.main.scale
testview.addSubview(result)
}
After I add result to testview as a subview it's disappeared.
Your Storyboard already added result into the view hierarchy so when you add it again the frame stays the same but the point of reference changes since the superview is now testView so in your case it's going beyond the visible bounds. If you want the UILabel to be a subview of the testView I'd recommend making that change directly on the Storyboard, creating the UILabel programmatically instead of using Storyboards/IBOutlets or updating the frame of the label after adding it to testView.

Can't change attributes of button in view loaded from nib file

I am loading a view which I created inside the interface builder like this:
let contentView = Bundle.main.loadNibnamed("ContentView", owner: nil, options: nil)?.first as! ContentView
The view contains an UIImageView and a UIButton. Both of them are connected with IBOutlets to the ContentView class file.
Now when I am loading the view I am setting the UIImageView's image which works fine. I am also trying to change the UIButton's text and backgroundColor but both won't change. Interestingly the button doesn't keep the color I set in the interface builder but the Text.
The code I am using to change the text and the color:
contentView.contentButton.backgroundColor = .red
contentView.contentButton.titleLabel?.text = "someText"
Thanks for the help.
ContentView.swift
class ContentView: UIView {
#IBOutlet weak var mainImageView: UIImageView!
#IBOutlet weak var contentImageView: UIImageView!
#IBOutlet weak var contentButton: UIButton!
}
The code I am using to create the views:
ContentScrollViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<imageArray.count {
let xPosition = Int(self.view.frame.width) * i
let contentView: ContentView = Bundle.main.loadNibNamed("ContentView", owner: nil, options: nil)?.first as! ContentView
contentView.frame.origin = CGPoint(x: xPosition, y: -Int((self.navigationController?.navigationBar.frame.height)!))
contentView.mainImageView.contentMode = .scaleAspectFill
contentView.contentImageView.image = #imageLiteral(resourceName: "stoerer_png_neu")
//Not working
contentView.specialButton.backgroundColor = .red
if let url = URL(string: imageArray[i]) {
contentView.mainImageView.af_setImage(withURL: url)
}
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(contentView)
}
}
You have to use
contentView.contentButton.setTitle("someText", for: .normal)
and
contentView.contentButton.setTitleColor(.red, for: .normal)
because you need to set those attributes for a specific UIControlState
For the background you need to use the layer:
contentView.contentButton.layer.backgroundColor = UIColor.red.cgColor
After restarting all files connected to the problem I finally managed to find the issue. Somehow the constraints of the UIButton in .xib were responsible for the issue. So if you are experiencing the same please check your constraints. :]

Scrollview just show my last array's element in view which i create with storyboard in swift 3?

I want to add a few view in scrollview. But I dont want to this view programmatically. I want to create with storyboard. This view include an image. I added scrollview and added to view. But in scrollview I just showing my last view. I want to 2 view and 2 image like my view. How can i fix this?
This is my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mainScrollView: UIScrollView!
var imageArray = [UIImage]()
#IBOutlet weak var myView: UIView!
#IBOutlet weak var imgv: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageArray = ["image","image2"]
for i in 0..<imageArray.count{
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
myView.layer.frame.size.width = myView.frame.width * CGFloat(i + 1)
imgv.image = imageArray[i]
mainScrollView.addSubview(myView)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
This is storyboard and controller looks like
You are having an issue with constraints from what I see and that is why you might not see every element that you put on your storyboard. You need to correct those errors for everything to show up correctly in your scrollview. Keep in mind, you need to tell the scrollview what its height and width should be in the storyboard. They need to be defined in some way. This is a common mistake when working with scrollviews.
You can put a view in your scrollview and define its height and width property. You can set its width to be equal to the screen's width and you can set a constant for your height.

Inserting UIView into background and having textfield/button appear on top (SWIFT)

I want to create a login page in which the image I have fills up the entire page. I was able to accomplish this by doing
let imageView = UIImageView(frame: self.view.bounds)
imageView.image = UIImage(named: "background")
self.view.addSubview(imageView)
However, I also have textfields and buttons on my storyboard which I created outlets for on my swift file. When I insert that code into the viewDidLoad() function, the textfields and login button go underneath the image rather than appear over the image. I'm not exactly sure how to get the login page text fields/buttons to appear over the UIImageView rather than be hidden underneath. The code I have for that is written outside the viewDidLoad
(example)
#IBOutlet var usernameTextField: UITextField!
#IBOutlet var passwordTextField: UITextField!
#IBAction func loginPressed() {
let username = usernameTextField.text
let password = passwordTextField.text
Try this:
self.view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
This method moves the specified view to the beginning of the array of views in the subviews property.

UIImageView Recognizer

In my app I have a big UIImageView and other 5 smaller.
My intention is to copy the content of the selected small Image to the biggest one. Also to get the name of the chosen image and show with a UILabel.
Will be maybe easier to replace the small images by UIButtons and display the image as a background?
I would add a gestureRecognizer to each imageView. The function called by the gestureRecognizer would then change the image on the "big picker view". There is no built-in way to get the image name that the user chose. If you really needed this, then you could sublcass UIImageView and add a fileName property.
#IBOutlet var imageView: UIImageView!
#IBOutlet var bigPickerImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// create tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: "tapGesture:")
// add it to the image view;
imageView.addGestureRecognizer(tapGesture)
// make sure imageView can be interacted with by user
imageView.userInteractionEnabled = true
}
func tapGesture(gesture: UIGestureRecognizer) {
// if the tapped view is a UIImageView then set it to imageview
if let imageView = gesture.view as? UIImageView { // if you subclass UIImageView, then change "UIImageView" to your subclass
// change the image on the bigPickerImageView
bigPickerImageView.image = imageView.image
// if you subclass UIImageView, then you could get the filename here.
}
}

Resources