Interesting Buttons - objective-c

Is there a way, without having to create images of my own, to make rounded rectangle buttons look more appealing? For example, providing them with shading, or making them look three dimensional.

Have a look at this article I wrote recently: UI Polish In An Instant
It talks about using layer properties to add visual interest to your UI with relative ease.

If you want to do so, then consider creating a UIView with all your customizations enabling it for user interaction. It might help!

Using Core Animation functions you can make them a bit more exciting. First import the QuartzCore library. Then you can access properties of the view.layer.
button.layer.cornerRadius changes the rounded corners
button.layer.strokeWidth changes the border thickness
button.layer.strokeColor changes the border colour
button.layer.shadowColor use this and other shadow properties to add a nice soft drop shadow
There's a whole bunch of properties you can tweak if you check the CALayer documentation, and these work for any UIView or subclass, not just buttons.
To add a gradient, check out the CAGradientLayer. You can create one of these and append it as a sublayer to your button.layer.
There's also CAShapeLayer to add an arbitrary polytonal shape to your button, all drawn using code without any images.

Related

Replace label in LineLimit with image for ios-charts

I have a line limit with a label. I would like to replace that label with an image. I realize the ios-charts library can't do that directly but was looking for some insight on how to do this.
There are several ways to do so:
Add the image outside of the ios-charts system, you can calculate the position of the image, and add it there;
You can subclass the ChartLimitLine, add a new property called UIImage, and then you can use it. You may subclass the chart view as a child chart view, and you can add your child limit line there, and then add the image to the view.
If the image is not complicated, such as simple lines and dots, you can choose to manually draw the shape using CoreGrahpics.
It's quite flexible for you to choose a proper way to do that. Just remeber you only need the origin point and the size, then you can add your image wherever you want.
Doing customization requires you understanding OOP and ios-charts in depth.

Full responsive UIView inside a UIScrollView using autolayout

I'm trying to understand how autolayout works under XCode6, but there's a lot of mysterious things that runs away from my mind. Autolayout and constraints philosofy can be very hard to learn, but I realized that life can be easier using these tools...
For your information, I need to build a chat view with a table (the messages) and a view containing a text field (the send message pane) nested in a UIView that is again nested in a UIScrollView, so I can shift up the scroll view as the keyboard appears under the textfield.
I read a lot of tutorials and watched a lot of video until I found the useful tutorial Using UIScrollView with Auto Layout in iOS. There's a Xcode project in Github of what the tutorial explains, too.
In his tutorial, Mike Woelmer tells that
One of the big pain points with the old way of setting up a
UIScrollView was communicating the content size to the scroll view. It
was fairly straightforward to calculate your content size if the
content in the UIScrollView was an image. But it was not as easy if
your scroll view included a mixed bag of buttons, labels, custom
views, and text fields. Lengthy code adjustments were needed to
reflect constant changes in device rotations and phone size
differences.
So Mike explains the way to adapt the UIView, using placeholder and forcing the view inside the scrollview to fits the device's screen, creating in viewDidLoad some NSLayoutConstraint:
The solution is to look outside the scroll view and attach a
constraint to the view controller’s main view. This cannot be done in
interface builder, so we will have to write some code. Interface
builder is still complaining, though, so we have to add a placeholder
width constraint to make it happy.
I tried to use parts of the code of the tutorial for my project, but I cannot get a working view controller for my needs (I always get errors). Which is the best approach to do this? Am I on the right road?
Last but not least, I'm italian, so pardon for my english. If something is not clear enough, please leave me a comment.
Basically you have to set both alignment and size constraints in order for Autolayout to take care of the rest for you. If you don't provide enough information you get warning. If you provide conflicting information you get errors.
You need basically to provide enough information for Autolayout to calculate the UIView frame property (i.e., x-position, y-position, width, height).
For example, by providing the distance constraints from the top, right, bottom, and left edges, Autolayout has enough information to draw that UIView's frame rectangle. But you could also provide just the distance constraints from the top and left edges and then provide a size and height constraint.
You can also configure the key constraints you need and then click 'resolve auto layout issues' and choose 'add missing constraints' though sometimes it doesn't give you what you want. It is better to understand that how Autolayout accomplishes what I described above.
If you mess up, it's usually easier to clear all the constraints and start over. Do it a few times and you'll get the hang of it.

How can I place a small image in my UIView

I want to place a series of images in a UIView... I want to be able to change each image's color using three different .png files (red,yellow,green) as a status indicator. Other than actually drawing the rectangle and filling it, is there an easier way?
Research effort: I have Googled and looked through SO and found nothing dealing with this.
There are lots of ways you can do this. I think the easiest way would be to pragmatically add UIViews as subviews to your main view, or to your parent UIView.
You can set the frame of your views as well as the background color. See this link
adding uiview as a subview to the main view
You can also use images, but since it is a plain solid color, its a lot of extra storage space, etc. to use images when you can programatically render a color.
If it is a series of rectangles, you can store UIViews in some kind of data structure so it is easier to dynamically change the color at runtime. If you aren't moving them around, then init there frames/geometry so they are in the correct location, then access them as members of an array or something similar.
If you want to add gradients to images, this is the best thing I have found:
Gradients on UIView and UILabels On iPhone
I figured it out... I took a UIButton, changed the Type to "Custom", sized it (32 x 32) and programmatically changed the the image depending on circumstances.
[button setImage:<#(UIImage *)#> forState:<#(UIControlState)#>]
Thanks to both of you for getting the thought process running... :D

UIButton border along rounded rect

I have a button and its type is UIButtonTypeRoundedRect, but its border does not go along the rounded corner. How can I curve the border.
Check out this blog post I recently wrote: UI Polish in An Instant
You're on the right track accessing the button's layer property, but there is more you need to do (for example, to get rounded corners, add deleteButton1.layer.cornerRadius = 10), and more you can do, all without extra images.
Images are the recommended method for creating custom buttons. Apple's built-in buttons are basically only there for prototyping.
You could also create a subclass of UIButton and then override the methods for drawInRect and provide custom drawing code, probably using CoreGraphics. However, it is still much cleaner in code and more efficient at runtime to just use images.

Programmatically added UI items order next to each other

There is a UIScrollView and I'd like to programmatically put subviews in it. It's ok, but how can I lay automatically the subviews next to each other?
Thanks in advance!
You can't. The view hierarchy in UIKit--like the view hierarchy in most UI frameworks--uses explicit layout.
Your comment asks about HTML-like floating; the box model stuff that HTML/CSS uses was designed to serve a very different goal (flowing document layout) than what UIKit is for, and so there's no real analogy in the framework.
UIViews do support automatic re-layout on resize/frame change of the parent view (you can say whether a subview should resize, how, and where it should be "pinned" to), through the autoresizing mask property, but I don't think that's what you're asking for.
You could, if you were inclined, build a set of methods, perhaps as a category on UIView, that let you add subviews that had their frame origins adjusted automatically based on existing subviews.
The position of the subviews is dictated by their frame property. So you need to set their frames such that they line up next to each other.
If they are all the same size you can do with some simple math and CGRectMake(). If they will have different sizes, you can use CGRectDivide() to break a large rect into smaller rects. CGRectInset() is also useful, in case you want some padding between them.