How to give UIImageView attributes using if/else function? - swift

How can I trim this function down to be as lean as possible? The only difference between the if and else blocks is the height of the UiImageView--if true, height is 300, else 150. I can't seem to figure out the best way to refactor this function without either redundant code (like in the example) or with a really long function that just seems way too long for something that seems rather simple.
I left the example crude to make the intention clear.
class ProfileViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
banner(profileHasCustomBanner: false)
}
// banner
func banner(profileHasCustomBanner: Bool) {
if profileHasCustomBanner == true {
let bannerImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 300))
bannerImageView.image = #imageLiteral(resourceName: "the-banner")
view.contentMode = .scaleAspectFit
view.addSubview(bannerImageView)
} else {
let bannerImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 150))
bannerImageView.image = #imageLiteral(resourceName: "the-banner")
view.contentMode = .scaleAspectFit
view.addSubview(bannerImageView)
}
}
}

Define a height variable based on the condition.
func banner(profileHasCustomBanner: Bool) {
let height: CGFloat = profileHasCustomerBanner ? 300 : 150
let bannerImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: height))
bannerImageView.image = #imageLiteral(resourceName: "the-banner")
view.contentMode = .scaleAspectFit
view.addSubview(bannerImageView)
}

Related

How to set contentSize for UIScrollView in Popover?

I have a UIScrollView in an UIViewController that is presented as Popover. On iPads the contentSize of the ScrollView is not calculated correctly as the view size is always the same as UIScreen.main.bounds.width. As the popover is smaller as the screen the paging fails as each slide is wider than the width of the popover width.
Here is how I load the popover:
let storyboard = UIStoryboard(name: "Onboarding", bundle: nil)
let onboardingVC = storyboard.instantiateViewController(identifier: "Onboarding")
if let popoverController = onboardingVC.popoverPresentationController {
popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
popoverController.sourceView = self.view
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
present(onboardingVC, animated: true, completion: nil)
And this is how I calculate the contentSize in the OnboardingViewController:
func setupSlideScrollView(slides : [Slide]) {
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: 1.0)
scrollView.isPagingEnabled = true
for i in 0 ..< slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(slides[i])
}
}
SOLUTION:
I think I found the solution myself. I had to call func setupSlideScrollView in override func viewWillLayoutSubviews() and not in override func viewDidLoad().

Unable to get an image to fit inside the bounds of a UITextField

I'm trying to get the image to scale down to the size of the UITextField and to position itself to the left side of it, but I am only able to get this:
What am I doing wrong?
Here is the relevant code:
//MARK: - Add Left Image to UITextField
func addLeftImageTo(txtField: UITextField, image img: UIImage) {
let leftImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
leftImageView.image = img
txtField.addSubview(leftImageView)
txtField.leftView = leftImageView
txtField.leftViewMode = .always
}
let image1 = UIImage(named: "stopwatch")
addLeftImageTo(txtField: timerTextField, image: image1!)
timerTextField.anchor(top: repsTextField.bottomAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: notesTextField.topAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 50.adjusted, left: 100.adjusted, bottom: 50.adjusted, right: 100.adjusted))
Try wrapping the UIImageView in a UIView
Your code:
func addLeftImageTo(txtField: UITextField, image img: UIImage) {
let leftImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
leftImageView.image = img
txtField.addSubview(leftImageView)
txtField.leftView = leftImageView
txtField.leftViewMode = .always
}
Example:
func addLeftImageTo(txtField: UITextField, image img: UIImage) {
let height = txtField.frame.size.height
let wrapView = UIView(frame: CGRect(x: 0, y: 0, width: height, height: height))
let leftImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: height, height: height))
leftImageView.image = img
leftImageView.contentMode = .scaleToFill
wrapView.addSubview(leftImageView)
txtField.addSubview(wrapView)
txtField.leftView = wrapView
txtField.leftViewMode = .always
}
Also, I added some lines including height, contentMode just in case.
I hope it will be helpful for you.
first make below method for adding image in UITextField
func setleftPaddingInTextfieldwithImage(_ textfield : UITextField, padding :
CGFloat, strImgname : String)
{
let imageView = UIImageView(image: UIImage(named: strImgname))
imageView.frame = CGRect(x: 0, y: 0, width: 15 , height: 15)
imageView.contentMode = .scaleAspectFit
let paddingView = UIView.init()
paddingView.frame = CGRect(x: padding, y: 0, width: padding, height: textfield.frame.size.height)
paddingView.addSubview(imageView)
textfield.leftView = paddingView
textfield.leftViewMode = .always
}
call it in your viewcontroller
self.setleftPaddingInTextfieldwithImage(YourTextField, padding: padding, strImgname: "stopwatch")

Change UIBarButton image from gallery

I have UIBarButton and user can change image just chose photo or gallery image with image picker. My problem is bad scale image.
If I use AspectFit my UIBarButton look like this:
If I use AspectFill my UIBarButton look like this:
and if I try first change size image and after set it, image all time scratched:
This is my code:
func createPhotoBarButton(image: Data) {
let barbtn = UIBarButtonItem()
var image = UIImage(data:image)
if image == nil {
image = UIImage(named: "photoIcon")
}
let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0))
imageView.image = image?.resizedImage(newSize: CGSize(width: 35, height: 35))?.withRenderingMode(.alwaysOriginal)
// imageView.image = cropToBounds(image: image!, width: 35, height: 35)
// imageView.image = image
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = imageView.frame.size.height / 2
imageView.layer.masksToBounds = true
imageView.clipsToBounds = true
self.navigationItem.rightBarButtonItem = barbtn
barbtn.customView = imageView
barbtn.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(photoTapped(_:))))
}
And here func for resize image:
func resizedImage(newSize: CGSize) -> UIImage? {
guard size != newSize else { return self }
let hasAlpha = false
let scale: CGFloat = 0.0
UIGraphicsBeginImageContextWithOptions(newSize, !hasAlpha, scale)
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
let newImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
Help me plz find the right way to resolving my problem.
As I understand your question. I found a solution.
Try this
func createPhotoBarButton(image: Data) {
let barbtn = UIBarButtonItem()
let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0))
var image = UIImage(data:image)
if image == nil {
image = UIImage(named: "photoIcon")?.resize(maxWidthHeight: Double(imageView.frame.size.width))
}
imageView.image = image
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = imageView.frame.size.height / 2
imageView.layer.masksToBounds = true
imageView.clipsToBounds = true
self.navigationItem.rightBarButtonItem = barbtn
barbtn.customView = imageView
barbtn.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(photoTapped(_:))))
}
Resize method
extension UIImage {
func resize(maxWidthHeight : Double)-> UIImage? {
let actualHeight = Double(size.height)
let actualWidth = Double(size.width)
var maxWidth = 0.0
var maxHeight = 0.0
if actualWidth > actualHeight {
maxWidth = maxWidthHeight
let per = (100.0 * maxWidthHeight / actualWidth)
maxHeight = (actualHeight * per) / 100.0
}else{
maxHeight = maxWidthHeight
let per = (100.0 * maxWidthHeight / actualHeight)
maxWidth = (actualWidth * per) / 100.0
}
let hasAlpha = true
let scale: CGFloat = 0.0
UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: maxHeight), !hasAlpha, scale)
self.draw(in: CGRect(origin: .zero, size: CGSize(width: maxWidth, height: maxHeight)))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
return scaledImage
}
}
Output
Try to create a custom button with the code below:
func createPhotoBarButton(image: Data) {
let imageBtnContainer = UIButton()
let imageBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
var image = UIImage(data:image)
if image == nil {
image = UIImage(named: "photoIcon")
}
imageBtn.setImage(image, forState: UIControlState.Normal)
imageBtn.frame = CGRectMake(0, 0, 53, 31)
imageBtn.imageView?.contentMode = .scaleAspectFit
imageBtnContainer.addSubview(imageBtn)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: imageBtnContainer)
...
}
You don't need anymore resizedImage(..) function
Here is working code (swift 4)
override func viewDidLoad() {
let containView = UIView(frame: CGRect(x: 0, y: 0, width: 35, height: 35))
let Button : UIButton = UIButton.init(type: .custom)
Button.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
Button.Round = true
Button.setImage(UIImage(named: "photoIcon"), for: .normal)
Button.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)
containView.addSubview(Button)
let rightBarButton = UIBarButtonItem(customView: containView)
self.navigationItem.rightBarButtonItem = rightBarButton
}
Button Action Here
#objc func btnTapped(_ sender: Any) {
print("Click Event")
}
Extension to Round
extension UIView{
#IBInspectable var Round:Bool{
get{
return false
}
set{
if newValue == true {
self.layer.cornerRadius = self.frame.size.height/2;
self.layer.masksToBounds = true;
}
}
}
}
Output:

UIPopoverPresentationController programmatically created fills the screen

The popup that I am creating programmatically fills the screen. As you can see from the example code, I have tried many ways to limit its size, but none are working. Also, the delegate methods are not being called. Any ideas? I have used CALayer successfully in the past for this kind of thing, but thought this would be simpler - maybe not.
#objc func touchDownHandler(sender: UISlider) {
let popoverController = UIViewController()
popoverController.view.backgroundColor = .red
popoverController.view.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
let textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
textLabel.text = "Hello World"
textLabel.backgroundColor = .green
popoverController.view.addSubview(textLabel)
popoverController.modalPresentationStyle = UIModalPresentationStyle.popover
popoverController.preferredContentSize = CGSize(width: 200, height: 200)
if let popoverPresentation = popoverController.popoverPresentationController {
popoverPresentation.delegate = self
popoverPresentation.sourceRect = sender.frame
popoverPresentation.popoverLayoutMargins = UIEdgeInsets(top: 10, left: 10, bottom: 210, right: 210)
popoverPresentation.backgroundColor = .blue
self.controller.present(popoverController, animated: true, completion: {
print("pop over is visible")
})
}
}
Keep in mind that, as per Apple documentation, "In a horizontally compact environment, the .popover option behaves the same as UIModalPresentationStyle.fullScreen."
https://developer.apple.com/documentation/uikit/uimodalpresentationstyle/popover

How to make tappable UIImages that run functions?(Swift)

(I just started using Swift a few days ago and am relatively new to programming, so please bear with me.) I am trying to make random blocks appear on the screen, and the user must tap them to make them disappear. I have been able to create the blocks, but I have no idea how to actually make them tappable. Can someone please help me? This is my code so far:
func createBlock(){
let imageName = "block.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: xPosition, y: -50, width: size, height: size)
self.view.addSubview(imageView)
UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
imageView.backgroundColor = UIColor.redColor()
imageView.frame = CGRect(x: self.xPosition, y: 590, width: self.size, height: self.size)
}, completion: { animationFinished in
imageView.removeFromSuperview()
self.life-=1
})
}
I want to make the block disappear when tapped; any suggestions on how I can do that?
It's extremely simple to do, just use a UITapGestureRecognizer. I typically initialize the recognizer as a global variable.
let tapRecognizer = UITapGestureRecognizer()
Then in your viewDidLoad:
// Replace with your function
tapRecognizer.addTarget(self, action: "removeBlock")
imageView.addGestureRecognizer(tapRecognizer)
func removeImage(gesture: UIGestureRecognizer) {
gesture.view?.removeFromSuperview()
}
func createBlock() {
let imageName = "block.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: xPosition, y: -50, width: size, height: size)
imageView.userInteractionEnabled = true // IMPORTANT
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "removeImage:"))
self.view.addSubview(imageView)
UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
imageView.backgroundColor = UIColor.redColor()
imageView.frame = CGRect(x: self.xPosition, y: 590, width: self.size, height: self.size)
}, { _ in
imageView.removeFromSuperview()
self.life-=1
})
}
Notice imageView.userInteractionEnabled = true it is really important because that will allow touch events on that view, it is set to false as defaul in UIImgageView.