How to add a circle shaped button with swift? - ios

I am trying to make this simple UI
Now the orange and darkblue are simple two views (the darkblue will have a nested tableview), but how would i make the button with some simple animation? Should i use CALayer or can i make use of the Interface Builder?

I would have done it through code:
let button2 = UIButton()
button2.frame = CGRectMake(0, 0, 100, 100)
button2.layer.borderColor = UIColor.whiteColor().CGColor
button2.layer.borderWidth = 2
button2.layer.cornerRadius = 50
button2.setTitle("button", forState: .Normal)
button2.backgroundColor = UIColor.blueColor()
button2.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
button2.setTitleColor(UIColor(red: 233/255, green: 64/255, blue: 87/255, alpha: 1), forState: UIControlState.Normal)
self.view.addSubview(button1)

Try This :-
Make sure your button's Height and width are the same
self.imgBg1.layer.cornerRadius = self.imgBg1.bounds.size.height / 2
self.imgBg1.layer.borderWidth = 3.0
self.imgBg1.layer.borderColor = UIColor.whiteColor().CGColor
self.imgBg1.clipsToBounds = true
self.imgBg1.contentMode = .ScaleToFill

Related

cannot set background = UIColor.clearColor() for UIBarButtonItem

I can set clearColor for UIButton
var btn = UIButton()
btn.backgroundColor = UIColor.clearColor()
But I can't set this for UIBarButtonItem.
You could either create a button and then use the UIBarButtonItem(customView: yourButton) init, or you could set the bar button item tintColor globally:
UIBarButtonItem.appearance().tintColor = UIColor.clearColor()
I presume you don't want to have a clear color for all your bar button items, so the first solution is probably the way to go here.
var customFilter: UIButton = UIButton(type: .Custom)
customFilter.setImage(UIImage(named: "filter.png")!, forState: .Normal)
customFilter.setTitle("Filter", forState: .Normal)
customFilter.setTitleColor(UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), forState: .Normal)
customFilter.sizeToFit()
customFilter.addTarget(self, action: #selector(self.filterTapped), forControlEvents: .TouchUpInside)
customFilter.layer.cornerRadius = 5
customFilter.backgroundColor = UIColor.yourcolor()
var btn: UIBarButtonItem = UIBarButtonItem(customView: customFilter)

How to add multiple subviews on view without hiding the last added subview?

This is my code
var button = [UIButton?](count:3, repeatedValue: UIButton())
for i in 0...2{
button[i]!.tag = i
button[i]!.frame = CGRectMake(CGFloat(i) *(collectionViewLove?.frame.width)!/3,0,(collectionViewLove?.frame.width)!/3, 30)
button[i]!.setTitle(titleForButtonsOneSelected[i], forState: .Normal)
button[i]!.titleLabel?.adjustsFontSizeToFitWidth = true
button[i]!.layer.borderWidth = 1.0
button[i]!.layer.borderColor = UIColor.blueColor().CGColor
button[i]!.backgroundColor = UIColor.clearColor()
button[i]!.setTitleColor(UIColor.blackColor(), forState: .Normal)
view.addSubview(button[i]!)
}
The problem is only the last button added to the view is getting shown. How do I show all the button objects in the view?
EDIT:
The frame values for UIButton I am getting
(0.0, 0.0, 106.666666666667, 30.0)
(106.666666666667, 0.0, 106.666666666667, 30.0)
(213.333333333333, 0.0, 106.666666666667, 30.0)
let buttons = Array(0...2).map { number -> UIButton in
let button = UIButton(frame: CGRectMake(CGFloat(number) * collectionViewLove!.frame.width / 3, 0, collectionViewLove!.frame.width / 3, 30))
button.tag = number
button.setTitle(titleForButtonsOneSelected[number], forState: .Normal)
buttontitleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 1.0
button.layer.borderColor = UIColor.blueColor().CGColor
button.backgroundColor = UIColor.clearColor()
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
view.addSubview(button)
return button
}
This way you have 3 different buttons in buttons. You can also customize the buttons right there as well.
Edit:
let buttons = Array(0...2).forEach {
let button = UIButton(frame: CGRectMake(CGFloat($0) * collectionViewLove!.frame.width / 3, 0, collectionViewLove!.frame.width / 3, 30))
button.tag = $0
button.setTitle(titleForButtonsOneSelected[$0], forState: .Normal)
buttontitleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 1.0
button.layer.borderColor = UIColor.blueColor().CGColor
button.backgroundColor = UIColor.clearColor()
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
view.addSubview(button)
}
It looks like button[i]!.frame = CGRectMake(CGFloat(i) *(collectionViewLove?.frame.width)!/3,0,(collectionViewLove?.frame.width)!/3, 30) is producing the same frame each time, so your buttons will be superimposed on each other. You need to have something dynamic in your call to have them be in different spots and visible.

iOS 9 UIButton Initialization

I am trying to initialize a UIButton programmatically but see one of these errors:
buttonWithType is unavailable: use object construction UIButton(type:)
or
error: incorrect argument label in call (have 'buttonWithType:', expected 'type:')
for char in keys {
let btn: UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
btn.frame = CGRectMake(0, 0, 20, 20)
btn.setTitle(char, forState: .Normal)
btn.sizeToFit()
btn.titleLabel?.font = UIFont.systemFontOfSize(20)
btn.backgroundColor = UIColor(hue: (216/360.0), saturation: 0.1, brightness: 0.81, alpha: 1)//
btn.setTitleColor(UIColor(white: 1.0, alpha: 1.0), forState: .Normal)
btn.setContentHuggingPriority(1000, forAxis: .Horizontal)
btn.setContentCompressionResistancePriority(1000, forAxis: .Horizontal)
btn.addTarget(self, action: Selector("handleBtnPress:"), forControlEvents: .TouchUpInside)
self.addSubview(btn)
}
The convenience initializer for UIButton has changed.
convenience init(type buttonType: UIButtonType)
Swift 3:
let btn = UIButton(type: .system) // note the lower case s
Swift 2.x:
let btn = UIButton(type: .System)
iOS 10 Xcode 8.....
let button = UIButton(type: .system) // note the lower case s
button.setTitle("Dismiss", for: UIControlState())
button.backgroundColor = UIColor.white
let buttonWidth: CGFloat = alertWidth/2
let buttonHeight: CGFloat = 40
button.frame = CGRect(x: alertView.center.x - buttonWidth/2, y: alertView.center.y - buttonHeight/2, width: buttonWidth, height: buttonHeight)
button.addTarget(self, action: #selector(ExampleIIIViewController.dismissAlert), for: UIControlEvents.touchUpInside)
alertView.addSubview(button)
In iOS 9 convenience init has been changed for buttonWithType. You cannot use
let button = UIButton.buttonWithType(UIButtonType.System)
instead use
let button = UIButton(type: UIButtonType.System)
Below is syntax :

How to make custom right UIBarButtonItem with image and label?

I actually found this same question here but for obj.c so with that I managed to do this:
trashNavButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
trashNavButton.addTarget(self, action: "trashItemClicked", forControlEvents: UIControlEvents.TouchUpInside)
trashNavButton = UIButton(frame: CGRectMake(0, 0, 50, 32))
trashNavButton.setTitle("\(trashCount)", forState: UIControlState.Normal)
trashNavButton.setTitleColor(UIColor(red: 229.0/255.0, green: 57.0/255.0, blue: 53.0/255.0, alpha: 1.0), forState: UIControlState.Normal)
trashNavButton.titleLabel?.textAlignment = NSTextAlignment.Right
trashNavButton.setImage(UIImage(named: "trashButton"), forState: UIControlState.Normal)
rightBarButton.customView = trashNavButton
self.navigationItem.rightBarButtonItem = rightBarButton
What I'm trying to do is to have a button on the right at the navigation bar, and I want that button to also have a label on it's left (you can see a great example on the question I mentioned above), and well this code works very well with the exception that the image is on the left and the label on the right and I want it to be the other way around, I'll appreciate your help :)
Just as an example
let containView = UIView(frame: CGRectMake(0, 0,70, 40))
let label = UILabel(frame: CGRectMake(0, 0, 50, 40))
label.text = "Right"
label.textAlignment = NSTextAlignment.Center
containView.addSubview(label)
let imageview = UIImageView(frame: CGRectMake(50, 10, 20, 20))
imageview.image = UIImage(named: "memoAccess")
imageview.contentMode = UIViewContentMode.ScaleAspectFill
containView.addSubview(imageview)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: containView)

UIButton does not have a member named setTranslatesAutoresizingMaskIntoConstraints

I am using xcode 7 beta 2 and getting the following error. How can I solve it?
UIButton does not have a member named setTranslatesAutoresizingMaskIntoConstraints
for char in keys {
let btn = UIButton(type: UIButtonType.System) as UIButton
btn.frame = CGRectMake(0, 0, 20, 20)
btn.setTitle(char, forState: .Normal)
btn.sizeToFit()
btn.titleLabel?.font = UIFont.systemFontOfSize(20)
btn.setTranslatesAutoresizingMaskIntoConstraints(false)
btn.backgroundColor = UIColor(hue: (216/360.0), saturation: 0.1, brightness: 0.81, alpha: 1)//
btn.setTitleColor(UIColor(white: 1.0, alpha: 1.0), forState: .Normal)
btn.setContentHuggingPriority(1000, forAxis: .Horizontal)
btn.setContentCompressionResistancePriority(1000, forAxis: .Horizontal)
btn.addTarget(self, action: Selector("handleBtnPress:"), forControlEvents: .TouchUpInside)
self.addSubview(btn)
}
translatesAutoresizingMaskIntoConstraints is a (boolean) property
of UIView. In Objective-C, you can use the "dot syntax" or the
setter method to assign a value to the property
btn.translatesAutoresizingMaskIntoConstraints = NO;
[btn setTranslatesAutoresizingMaskIntoConstraints:NO];
but in Swift you just assign a new value to the property:
btn.translatesAutoresizingMaskIntoConstraints = false

Resources