I want my image to be in the center in the x axis, I'm writing this code :
let emptyImage=UIImageView(frame: CGRect(x: (UIScreen.main.bounds.width)/2 ,y: 200 , width: 50, height: 50))
but it doesn't work.
try this
let emptyImage = UIImageView(frame: CGRect(x: UIScreen.main.bounds.width/2 - 50/2, y: 200, width: 50, height : 50)
As you are trying to create image in code. Try adding anchors between imageView and tableview. Find below peace of example code.
let emptyImage = UIImageView(image: UIImage(named: ""))
view1.backgroundColor = UIColor.black // view1 consider this as your tableview
emptyImage.translatesAutoresizingMaskIntoConstraints = false //missed it in first place
self.view1.addSubview(emptyImage)
NSLayoutConstraint.activate([
emptyImage.centerXAnchor.constraint(equalTo: view1.centerXAnchor),
emptyImage.centerYAnchor.constraint(equalTo: view1.centerYAnchor),
emptyImage.heightAnchor.constraint(equalToConstant: 100),
emptyImage.widthAnchor.constraint(equalToConstant: 100)
])
Hope this helps!
Try setting imageView center property to tableView.center:
imageView.center = tableView.center
If you want to show an UIImageView instead of cells, then try this:
let emptyImage=UIImageView(frame:
CGRect(x: 0,
y: 0,
width: self.tableView.bounds.size.width,
height: self.tableView.bounds.size.height))
self.tableView.backgroundView = emptyImage
Related
I have added a UIImageView to my UITableView Header and now I am trying to center is, but it won't center, it goes to the right.
let imageHeader = UIImageView(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: 50))
let header = UIView(frame : CGRect(x : 0, y : 0, width : self.tableView.frame.width, height : 200))
imageHeader.image = UIImage(named: "paindown-logo.png")
imageHeader.contentMode = .scaleAspectFit
imageHeader.center = CGPoint(x: header.bounds.midX, y: header.bounds.midY);
header.addSubview(imageHeader)
self.tableView.tableHeaderView = header
What am I doing wrong? Why will not center?
reload your imageHeader view then reload your table view after open menu.
I am trying to create the following registration form . I cant set the space between the textfield image and the placeholder text . I've tried the following :
Email_text.placeholderRect(forBounds: CGRect(x: 10, y: 0 , width: 60, height: 50))
any idea how can I fix this ?
Thanks
You can simply do like this, an extension for UITextField
extension UITextField {
func setTextIconAndPlaceholder(icon: UIImage, placeholder: String) {
let imageView = UIImageView()
imageView.image = icon
imageView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
let view = UIView()
view.frame = CGRect(x: 0, y: 2, width: 25, height: 25)
view.backgroundColor = UIColor.clear
view.addSubview(imageView)
self.leftView = view
self.leftViewMode = UITextFieldViewMode.always
self.placeholder = placeholder
}
}
I've updated to Xcode 9, and I have a titleView in my NavigationItem created in this way:
let logo = UIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 25))
logo.image = UIImage.logo
logo.contentMode = .scaleAspectFit
self.navigationItem.titleView = logo
The result is that it doesn't consider anymore the frame height.
we can control the size and position of UINavigationbar titleview. Don't use to set the imageview as titleview directly. in here create a custom UIView and then set the frame as what you need requirement and add the logo as its subview
do like
let supportVie = UIView(frame: CGRect(x: 0, y: 0, width: 70, height: 25))
// Here you can set View width and height as per your requirement for displaying supportVie position in navigationbar
//supportVie.backgroundColor = UIColor.red
let logo = UIImageView(image: UIImage.logo ) //UIImage(named: "SelectAnAlbumTitleLettering")
logo.frame = CGRect(x: 45, y: 5, width: supportVie.frame.size.width, height: supportVie.frame.size.height)
// customize the origin as (45,5) but can pass them as your requirement.
supportVie.addSubview(logo)
//supportVie.contentMode = .center;
navigationItem.titleView = supportVie
I'm placing the background image this:
self.navigationController.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
but, repeat the image. No I want it to be repeated to me and Put it center.. ?
Here's how to fix it using UIAppearance and UIImage API:
// disable image stretching by defining left and top caps.
let navbarImage = image.stretchableImage(withLeftCapWidth: 1, topCapHeight: 1)
UINavigationBar.appearance().setBackgroundImage(navbarImage, for: .default)
Example before:
and after the fix:
Try with below method which worked for me.
var headerview = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
let imgview = UIImageView(frame: CGRect(x: 75, y: 0, width: 150, height: 44))
imgview.image = UIImage(named: "ImageName")
imgview.contentMode = UIViewContentMode.scaleAspectFit
headerview.addSubview(imgview)
self.navigationController?.navigationBar.topItem?.titleView = headerview
I have a UIView that holds a UILabel inside.
After applying affine transform on the UIView using:
myView.transform = CGAffineTransformMakeScale(4, 4);
My UILabel (which is a sub view to myView) grows as well.
Is there a way to prevent this?
i tried:
1) Using the CGAffineTransformIdentity flag on the label.
2) Adding a superview to myView and adding myView as superview's subview, and the label as a subview to the superview (and not myView).
Non of them seem to be working, the label keeps growing.
Any ideas?
You answered your own question with option 2. Not sure why it's not working since you did not supply any code. The playground code below shows it will work. Uncomment out the last line to transform the subview but not the label.
import UIKit
import XCPlayground
let superview = UIView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))
XCPlaygroundPage.currentPage.liveView = superview
superview.backgroundColor = UIColor.redColor()
let view = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
view.backgroundColor = UIColor.greenColor()
superview.addSubview(view)
let label = UILabel(frame: CGRect(x: 20, y: 10, width: 40, height: 40))
label.text = "Hello"
superview.addSubview(label)
//view.transform = CGAffineTransformMakeScale(2, 2)