{"id":127,"date":"2013-12-14T17:56:58","date_gmt":"2013-12-14T23:56:58","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=127"},"modified":"2020-05-21T18:37:00","modified_gmt":"2020-05-21T23:37:00","slug":"creating-attractive-flat-buttons-on-ios-7","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/creating-attractive-flat-buttons-on-ios-7\/","title":{"rendered":"Creating Attractive Flat Buttons on iOS 7"},"content":{"rendered":"<p><b>Update!<\/b> &#8211; If you are interested in learning more about Apple&#8217;s new Swift programming language, we have a version of using FlatButton with Swift over <a href=\"https:\/\/dev.iachieved.it\/iachievedit\/?p=225\">here<\/a>.  Enjoy!<\/p>\n<p>Well, it&#8217;s certainly been a while since our last post. \u00a0We&#8217;ve been busy working on a variety of projects, but things have slowed down a bit as the 2013 holiday season approaches so here we are with a simple tutorial on creating attractive &#8220;flat&#8221; buttons on iOS.  For the impatient that want the code, everything below is in an XCode project hosted on <a href=\"https:\/\/github.com\/mojojoseph\/flatbutton\">Github<\/a>.<\/p>\n<p>While many of our tutorials assume you&#8217;ve done no iOS programming in the past, this one will assume you are familiar with how to create a new iOS application, add 3rd-party frameworks and code, etc. \u00a0We&#8217;ll be using Jason Everett&#8217;s FlatButton class, which you can download from\u00a0<a href=\"https:\/\/github.com\/ijason\/FlatButtons\">Github<\/a>. \u00a0You only need to include <code>FlatButton.m<\/code> and <code>FlatButton.h<\/code> in your project, but make sure and link to the QuartzCore framework.<\/p>\n<p>After creating your project and including <code>FlatButton.h<\/code> and <code>.m<\/code> (and link to QuartzCore!), add the following to your <code>ViewController.m<\/code> file:<\/p>\n<p>[objc]<br \/>\n#import &quot;FlatButton.h&quot;<br \/>\n[\/objc]<\/p>\n<p>and<\/p>\n<p>[objc]<br \/>\n-(void)viewWillAppear:(BOOL)animated {<br \/>\n  [super viewWillAppear:animated];<br \/>\n  UIButton* flatButton = [[FlatButton alloc] initWithFrame:CGRectMake(20,350,280,40)<br \/>\n                                             withBackgroundColor:[UIColor colorWithRed:0.521569<br \/>\n                                                                                 green:0.768627<br \/>\n                                                                                  blue:0.254902 alpha:1]];<br \/>\n  flatButton.layer.cornerRadius = 10;<br \/>\n  [flatButton setTitle:@&quot;Sign In&quot; forState:UIControlStateNormal];<br \/>\n  flatButton.titleLabel.font = [UIFont fontWithName:@&quot;Avenir-Black&quot; size:20.0f];<br \/>\n  [flatButton addTarget:self action:@selector(flatButtonPressed:) forControlEvents:UIControlEventTouchUpInside];<br \/>\n  [self.view addSubview:flatButton];<br \/>\n}<\/p>\n<p>-(void)flatButtonPressed:(id)button {<br \/>\n  NSLog(@&quot;flatButtonPressed&quot;);<br \/>\n}<br \/>\n[\/objc]<\/p>\n<p>A brief discussion on what&#8217;s going on here. First, rather than drag-n-drop of your button from XCode onto the view controller canvas, you&#8217;re programmatically creating the button. <code>FlatButton<\/code> inherits from <code>UIButton<\/code> so you can assign directly to a <code>UIButton*<\/code>. When creating UI elements programmatically you have to explicitly set things that XCode did for you when using NIBs or storyboards. For example, above we declare the where and what of the button layout with <code>CGRectMake<\/code>, as well as the details of the background color of the button.<\/p>\n<p>To add additional appeal we use the <code>cornerRadius<\/code> attribute of the <code>UIButton<\/code>&#8216;s <code>layer<\/code> object. Setting this to 10 as shown above will give the button&#8217;s corners a nice rounded effect.<\/p>\n<p>Finally, we set the font name and size of the button label, add the method that we want called when the button is pressed (<code>UIControlEventTouchUpInside<\/code>), and then add the button to our view controller&#8217;s view!<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/12\/flatbutton.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-138\" alt=\"flatbutton\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/12\/flatbutton.png\" width=\"384\" height=\"576\" \/><\/a><\/p>\n<p>That&#8217;s really all there is to it. The &#8220;hard part&#8221; is programming the button placement on an iPhone 4 vs. an iPhone 5 (perhaps another blog post or we&#8217;ll extend this one). Everything else is handled nicely by the <code>FlatButton<\/code> class. For more details on <code>FlatButton<\/code> see <a href=\"http:\/\/blog.ijasoneverett.com\/2013\/06\/flat-buttons-an-ios-control-for-a-flat-ui\/\">Jason Everett&#8217;s blog<\/a>.<\/p>\n<p>Don&#8217;t forget you can follow us on <a href=\"https:\/\/twitter.com\/iachievedit\">Twitter<\/a>, where you&#8217;ll get the latest updates on our iOS tutorials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update! &#8211; If you are interested in learning more about Apple&#8217;s new Swift programming language, we have a version of using FlatButton with Swift over here. Enjoy! Well, it&#8217;s certainly been a while since our last post. \u00a0We&#8217;ve been busy working on a variety of projects, but things have slowed down a bit as the [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-xcodetips"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/127"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":10,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":4002,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/127\/revisions\/4002"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}