UIImage.draw() function not working in viewDidLoad() callback [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In the viewDidLoad() callback below, why does the image?.draw() function not work?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named:"0")
image?.draw(in: CGRect(x: 0, y: 0, width: 100, height: 100))
}
// more code here
}

The method of a UIImage draws a image at a current graphic context. In the method viewDidLoad no a graphic context.
Move the calling of the drawing into draw method of some custom view.

Related

Swift: Best way to manage UI elements, such as UIButtons? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Swift.
I'm trying to make template files for all elements that I often use such as buttons and labels.
Can you tell me the best way manage the templates like that has the appearance like including the colors, corner radius?
I cannot figure out except using Class files.
Thanks!
You have 2 ways to do it.
1 - create your own class
final class PrimaryButton: UIButton {
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = UIColor.red
setTitleColor(UIColor.black, for: .normal)
}
}
2 - create extension to configure
extension UIButton {
func style() {
backgroundColor = R.color.primary()
setTitleColor(R.color.black(), for: .normal)
}
}
and call this function in viewDidLoad
first style is preferred
Personally, I like to create a new file called "Styling" with the same-named class.
Then, inside there, I create static functions that accept parameters that you want to edit (for example, button).
class Styling {
static func styleButton(_ button: UIButton) {
button.layer.cornerRadius = 25.0
}
}
Then I just call it by typing in vc:
Styling.styleButton(myButton)
To add to the above answers what works for me is putting series of buttons in an array of UIButtons. It makes it much simples to rearrange them while developing the app or adjusting to various screens.
Here is a simplified example of 12 buttons forming a colorful flag:
var flagButton: [UIButton] = []
for i in 0...11 {
flagButton.append(UIButton())
flagButton[i].frame = CGRect(x: x, y: y, width: width, height: height)
self.view.addSubview(flagButton[i])
}
You can create a class like this:
class CustomButton: UIButton {
override func awakeFromNib() {
super.awakeFromNib()
self.layer.cornerRadius = 20
}
}
And use it:

Display multiple buttons side-by-side by using single button iOS Swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In stackoverflow is showing only 2 or 3 button ie., through storyboard
Images: https://ibb.co/gSFQX2k
var consolebtn = [UIButton]()
var x = Int()
x = 0
#IBAction func pushButton(sender: UIButton) {
let btn = UIButton()
btn.frame = CGRect(x:x, y:10, width:10, height:10)
btn.setTitle("Button Title",for: .normal)
consolebtn.append(btn)
view.addSubview(btn)
x += 20 // 10 is width & 10 is gap between two button
}
Try going through blog, It uses collection view to solve the problem
https://medium.freecodecamp.org/how-to-make-height-collection-views-dynamic-in-your-ios-apps-7d6ca94d2212

How to render any UIView into a UIImage [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to render any UIView into a UIImage with Swift in iOS
The code presented below can render any UIView into a UIImage:
let view = UIView()
let imageRenderer = UIGraphicsImageRenderer(size: view.bounds.size)
let renderedImage = imageRenderer.image {ctx in
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}
The second part of the task, sharing an image to a social platform, should be posted as a separate question.

How to add something on an image in swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an anatomic picture and on it, I want to print a image like a dot or something when the user taps on the first image (the body) to point out where does it hurts.
I've already read something on UITapGestureRecognizer, but I don't really understood how it works.
Try this:
override func viewDidLoad() {
super.viewDidLoad()
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap))
self.imageView.addGestureRecognizer(gestureRecognizer)
}
#objc func handleTap(tap: UITapGestureRecognizer) {
let circle = UIView()
circle.center = tap.locationInView(imageView)
circle.frame.size = CGSize(width: 30, height: 30)
circle.layer.backgroundColor = UIColor.redColor().CGColor
circle.layer.cornerRadius = 15
imageView.addSubview(circle)
}

Swift, Pass data from popover controller to previous controller [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i want to fill text View in current controller from data existing in popover controller
with out reload data in current controller
can i do it ??
You can use custom delegate method to fill the value.
class PopupviewController {
weak var delegate: filleTextViewDelegate?
func calldelegate () {
delegate?.fillTextview(filledData)
}
}
protocol filleTextViewDelegate : class {
func fillTextview(filledData: String)
}
class CurrentViewController :filleTextViewDelegate {
func fillTextview(filledData: String) {
textView.text = filledData
}
}

Resources