Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ben Bastow
24,657 PointsSetting a button as an image made in code?
hai! I have got a button that I have linked via a iboutlet and i wanted to set the image from the function I set but it isn't working and saying it won't work or something can you help please?
Here is the code in full:
import UIKit
class MasterViewController: UIViewController {
@IBOutlet weak var aButton: UIButton!
override func viewDidLoad() {
aButton.setImage(Start(CGRect(x: 137, y: 269, width: 100, height: 100)),forState: UIControlState.Normal)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Start(rect:CGRect){
let ctx = UIGraphicsGetCurrentContext()
let width = rect.width
let height = rect.height
var pstyleRightAlign = NSMutableParagraphStyle()
pstyleRightAlign.alignment = NSTextAlignment.Right
var pstyleLeftAlign = NSMutableParagraphStyle()
pstyleLeftAlign.alignment = NSTextAlignment.Left
var pstyleCenterAlign = NSMutableParagraphStyle()
pstyleCenterAlign.alignment = NSTextAlignment.Center
//Line_Copy
CGContextSaveGState(ctx)
CGContextMoveToPoint(ctx, 76.03999999999999*(width/124), 62.5*(height/124))
CGContextAddCurveToPoint(ctx, 76.03999999999999*(width/124), 62.5*(height/124), 47.97*(width/124), 62.5*(height/124), 47.97*(width/124), 62.5*(height/124))
CGContextAddCurveToPoint(ctx, 47.97*(width/124), 62.5*(height/124), 76.03999999999999*(width/124), 62.5*(height/124), 76.03999999999999*(width/124), 62.5*(height/124))
CGContextSetStrokeColorWithColor(ctx, UIColor(hue: 0, saturation: 0, brightness: 1, alpha: 1).CGColor)
CGContextSetLineWidth(ctx, 8*min(width/124, height/124))
CGContextDrawPath(ctx, kCGPathStroke)
CGContextRestoreGState(ctx)
//Path
CGContextSaveGState(ctx)
CGContextMoveToPoint(ctx, 62*(width/124), 48.5*(height/124))
CGContextAddCurveToPoint(ctx, 62*(width/124), 48.5*(height/124), 62*(width/124), 76.57*(height/124), 62*(width/124), 76.57133674621582*(height/124))
CGContextAddCurveToPoint(ctx, 62*(width/124), 76.57*(height/124), 62*(width/124), 48.5*(height/124), 62*(width/124), 48.5*(height/124))
CGContextSetStrokeColorWithColor(ctx, UIColor(hue: 0, saturation: 0, brightness: 1, alpha: 1).CGColor)
CGContextSetLineWidth(ctx, 8*min(width/124, height/124))
CGContextDrawPath(ctx, kCGPathStroke)
CGContextRestoreGState(ctx)
//Path
CGContextSaveGState(ctx)
CGContextMoveToPoint(ctx, 62*(width/124), 124*(height/124))
CGContextAddCurveToPoint(ctx, 96.24*(width/124), 124*(height/124), 124*(width/124), 96.24*(height/124), 124*(width/124), 62*(height/124))
CGContextAddCurveToPoint(ctx, 124*(width/124), 27.76*(height/124), 96.24*(width/124), 0*(height/124), 62*(width/124), 0*(height/124))
CGContextAddCurveToPoint(ctx, 27.76*(width/124), 0*(height/124), 0*(width/124), 27.76*(height/124), 0*(width/124), 62*(height/124))
CGContextAddCurveToPoint(ctx, 0*(width/124), 96.24*(height/124), 27.76*(width/124), 124*(height/124), 62*(width/124), 124*(height/124))
CGContextSetStrokeColorWithColor(ctx, UIColor(hue: 0, saturation: 0, brightness: 1, alpha: 1).CGColor)
CGContextSetLineWidth(ctx, 8*min(width/124, height/124))
CGContextDrawPath(ctx, kCGPathStroke)
CGContextRestoreGState(ctx)
}
}
The part I'm having trouble with is :
@IBOutlet weak var aButton: UIButton!
override func viewDidLoad() {
aButton.setImage(Start(CGRect(x: 137, y: 269, width: 100, height: 100)),forState: UIControlState.Normal)
I would be grateful if you could help!!
what I am trying to do is to set the button that i have dragged onto the view as an image. and the image i have made was that function called start. but i don't know how to make start the image of the button.
Pasan Premaratne or Amit Bijlani Can you help? Im still really stuck on this oops >.<
1 Answer
agreatdaytocode
24,757 PointsGive this try and tell me if it works for you.
let image = UIImage(named: "ImageName.png") as UIImage
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button .setBackgroundImage(image, forState: UIControlState.Normal)
button.addTarget(self, action: "Action:", forControlEvents:UIControlEvents.TouchUpInside)
menuView.addSubview(button)
Ben Bastow
24,657 PointsBen Bastow
24,657 Pointshello!! I tried this just now :) and it doesn't work either :p but what I am trying to do is to set the button that i have dragged onto the view as an image. and the image i have made was that function called start. but i don't know how to make start the image of the button.