UIButton does not have a member named setTranslatesAutoresizingMaskIntoConstraints - ios

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

Related

Set image and title both on RightBarButton

I want to set image and title both on rightBarButton. Title will come first. I searched a lot but all the articles are related to leftBarButton. So can anyone please tell me, how to set both of them?
func methodForSettingButtonClicked() {
let buttonSetting = UIButton(type: .custom)
buttonSetting.frame = CGRect(x: 0, y: 5, width: 25, height: 25)
let imageReg = UIImage(named: "setting")
buttonSetting.setTitle("Settings", for: .normal)
buttonSetting.setImage(imageReg, for: .normal)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = buttonSetting
self.navigationItem.rightBarButtonItem = leftBarButton;
}
try barbutton item with custom VIew as follows:-
let button = UIButton(type: .Custom)
button.setImage(UIImage(named: "icon_right"), forState: .Normal)
button.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
button.frame = CGRectMake(0, 0, 53, 31)
button.imageEdgeInsets = UIEdgeInsetsMake(-1, 32, 1, -32)//move image to the right
let label = UILabel(frame: CGRectMake(3, 5, 50, 20))
label.font = UIFont(name: "Arial-BoldMT", size: 16)
label.text = "title"
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
label.backgroundColor = UIColor.clearColor()
button.addSubview(label)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
imageEdgeInsets plays a important role.
Try it as below and set title as you want
let button = UIButton(type: .system)
button.setImage(UIImage(named: "icon_image_name"), for: .normal)
button.setTitle("Categories", for: .normal)
button.addTarget(self, action: #selector(clickOnButtonActionMethod), for: .touchUpInside)
button.sizeToFit()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
If you want then change button property as you want.
You can do like this way,
let button = UIButton(type: .custom)
button.setTitle("Settings", for: .normal)
button.setImage(#imageLiteral(resourceName: "settings"), for: .normal)
button.semanticContentAttribute = .forceRightToLeft
button.setTitleColor(UIColor.white, for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
It works for me, hope it can help you.

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 a circle shaped button with swift?

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

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 :

Cannot change textColor and font size inside the loop in swift

I have no idea why the font size and text Color doesn't takes any effect in the loop...anyone help??
Here is my code:
var buttons = ["N","F","S","D","C","A","R"]
for button in buttons
{
var Button:UIButton = UIButton()
Button = UIButton(frame: CGRect(x:10, y: 20, width: 80, height: 80))
Button.frame.origin = ButtonPosition
ButtonPosition.x = ButtonPosition.x + buttonIncrement
Button.layer.cornerRadius = 3
Button.clipsToBounds = true
Button.backgroundColor = UIColor.groupTableViewBackgroundColor()
Button.setTitle("\(button)", forState: UIControlState.Normal)
Button.titleLabel?.text = "\(button)"
Button.titleLabel?.font = UIFont(name:"Chalkboard SE Regular", size: 30)
Button.titleLabel?.textColor = UIColor.blackColor()
Button.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#b8b4af", alpha: 0.5)), forState: .Highlighted)
Button.addTarget(self, action: "check:", forControlEvents: UIControlEvents.TouchUpInside)
buttonView.addSubview(Button)
}
return buttonView
First remove the line Button.titleLabel?.text = "\(button)".
This one is enough Button.setTitle("\(button)", forState: UIControlState.Normal).
Then you should use setTitleColor method from a button to change it.
Button.setTitleColor(UIColor. blackColor(), forState: UIControlState.Normal)
Finally you made a mistake in the name of your font :
Button.titleLabel?.font = UIFont(name:"ChalkboardSE-Regular", size: 30.0)
That's it!
You should edit directly the button, not the titleLabel, like this:
Button.setTitle("\(button)", forState: .Normal)
Button.setTitleColor(UIColor.blackColor(), forState: .Normal)

Resources