I'm trying to create custom popopview using labels and button, something like this;
First, created a label with opac background;
let opacLabel = UILabel(frame:CGRectMake(0,0,self.screenWidth,self.screenHeight))
opacLabel.backgroundColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.9)
view.addSubview(opacLabel)
then a layer for popup view;
let categoryMenuLabel = UILabel(frame: CGRectMake(10,40,100,300))
categoryMenuLabel.backgroundColor = UIColor.whiteColor()
categoryMenuLabel.clipsToBounds = true
categoryMenuLabel.layer.cornerRadius = 7
and then a button
let saveButton = UIButton(frame: CGRectMake(spaceX,400,150,40))
saveButton.backgroundColor = UIColor.lightGrayColor()
saveButton.titleLabel!.font = UIFont(name: "Helvetia", size: 25)
saveButton.setTitle("Save", forState: UIControlState.Normal)
saveButton.addTarget(self, action: "categorySaveButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
categoryMenuLabel.addSubview(saveButton)
finally added categoryMenuLabel to main view and changed order;
self.view.addSubview(categoryMenuLabel)
self.view.bringSubviewToFront(opacLabel)
self.view.bringSubviewToFront(categoryMenuLabel)
With this configuration, when I try to press button, instead of button, it pressed the table cell under label.
I also couldn't manage to disable touch actions outside of the label.
Just enable user interaction on the label. It's off by default, and that prevents it's children from receiving any user interaction, as well:
categoryMenuLabel.userInteractionEnabled = true
Related
In short, how can I change the color of the black button items (while maintaining control over their size)?
Longer version: I am programmatically adding a number of custom UIBarButtonItems to a UIToolbar. I'm using the solution found here so that I can control the size of the items.
While this fixes the UIBarButtonItem size issue, it does not respect tintColor like a typical UIBarButtonItem would. So I get something like this:
The large white item is the color I want, but not the size. This item was simply added in IB with the tintColor set to default.
The small black items are the size I want, and were added with the following code (N.B. the lines marked with // Not producing intented result):
for e in (self.profile?.expressions)! {
let button = UIButton()
button.setImage(UIImage(named: "emojiph"), for: .normal)
button.addTarget(self, action: #selector(onEmojiInsert), for: .touchUpInside)
let barItem = UIBarButtonItem(customView: button)
barItem.tag = e.family_expression_id
let wConstraint = barItem.customView?.widthAnchor.constraint(equalToConstant: 32)
wConstraint?.isActive = true
let hConstraint = barItem.customView?.heightAnchor.constraint(equalToConstant: 32)
hConstraint?.isActive = true
// Not producing intented result
button.tintColor = UIColor.white
// Not producing intented result
barItem.customView?.tintColor = UIColor.white
// Not producing intented result
barItem.tintColor = UIColor.white
self.emojibar.items?.append(barItem)
// Add flexible spacers
if (e.family_expression_id < (self.profile?.expressions.count)!) {
self.emojibar.items?.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil))
}
}
A workaround would be to provide the white image assets, but I'd prefer to find a more elegant solution if it exists.
To make the button change the color of the presented image to match the tint color then you need to initiate the button as a system type
let button = UIButton(type: .system)
I am trying to change the image on my button depending on the position of a switch. I am not sure how to do this. I am relatively new to code. I have managed to change the other things on the screen but not the button. I have attached my code. I put this function into the IBAction for the switch:
func updateMySwitchState(){
if darkModeSwitch.isOn {
self.view.backgroundColor = UIColor.black
removeAds.textColor = UIColor.white
aboutText.textColor = UIColor.white
about.textColor = UIColor.white
backButton.setImage(UIImage(named: "backinvert-40"), for: .normal)
} else {
self.view.backgroundColor = UIColor.white
removeAds.textColor = UIColor.black
aboutText.textColor = UIColor.black
about.textColor = UIColor.black
backButton.setImage(UIImage(named: "back-40"), for: .normal)
}
}
Did you connected the outlet of the button with the controller?
You can set both images button image in storyboard itself for normal & selected state respectively. After setting call
backButton.setSelected(true) for first image and
backButton.setSelected(false) for the second
I have a button that I create in interface builder that highlights the entire button on tap. I do nothing in code, simply ctrl+drag to my view controller file.
Then I have another button that I create programatically with this code.
let goToButton = UIButton()
goToButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
goToButton.titleLabel?.font = UIFont(name: "Arial", size: 18)
goToButton.setTitle("◀︎ Go To Form", forState: .Normal)
goToButton.backgroundColor = UIColor.whiteColor()
goToButton.showsTouchWhenHighlighted = true // Should highlight entire button?
but on tap it look like this.
How can I make it so that it matches the highlight like a button from interface builder
set your button type as Custom or `System and try once , change this
let goToButton = UIButton()
into
let goToButton = UIButton(type:.System)
or
let goToButton = UIButton(type:.Custom)
and hide this
goToButton.showsTouchWhenHighlighted = true
I have a scrollview inside a view. Inside the scrollview I create programmatically 5 buttons. Every button loads a different image with a different tag each one. I added a function that is called when pressing the buttons.
let avatarsListScrollingView = avatarsListView(CGSizeMake(70.0, 55.0), avatarCount: 5)
func avatarsListView(buttonSize:CGSize, avatarCount:Int) -> UIView {
**CODE**
for i in 0...(avatarCount-1) {
let button = UIButton(type: .Custom)
**CODE**
button.setImage(UIImage(named: avatarsList[i]), forState: .Normal)
button.tag = i
button.addTarget(self, action: "avatarListSelected:", forControlEvents: .TouchUpInside)
avatarButtonView.addSubview(button)
}
return avatarButtonView
}
Then when pressing the buttons, I call to "avatarListSelected":
func avatarListSelected(sender:UIButton){
if let image = sender.imageView?.image?.imageWithRenderingMode(.AlwaysTemplate) {
sender.setImage(image, forState: .Normal)
sender.tintColor = UIColor.redColor()
}
self.addAvatarView.reloadInputViews()
}
This function tints the image button to red, it is working fine, but the problem is that when I press some other button, I want the other one goes to the original color. Right now every button that I press gets in red.
I tried to add the call to "self.addAvatarView.reloadInputViews()" to try to "redraw" again all the buttons, but never gets called.
Do you guys know some way to do this?
Thanks to everybody!
This is the final code that solved the problem:
func avatarListSelected(sender:UIButton){
print(sender.tag)
if let image = sender.imageView?.image?.imageWithRenderingMode(.AlwaysTemplate) {
sender.setImage(image, forState: .Normal)
sender.tintColor = UIColor.redColor()
}
for view in self.avatarButtonView.subviews as [UIView] {
if let btn = view as? UIButton {
if btn.tag != sender.tag {
btn.setImage(UIImage(named: avatarsList[btn.tag]), forState: .Normal)
}
}
}
}
Create a property selectedButton: UIButton? and keep a reference to the selected button there. Don't forget to update it in avatarListSelected method and before you change it, if it isn't nil, change its color to original (and then change it).
If the buttons have different original colors, subclass UIButton class and keep the original color there.
I don't know if is better approach or answer, but, i maybe could delivery this using this approach:
Create a method that will "fill" the color for your choice button and "clear" color to others , but its a method that loop through UIScrollView and look for each UIButton. Something like this :
func setBackgroundColorButton(color:UIColor , buttonTag:Int){
for view in self.scrollView.subviews as [UIView] {
if let btn = view as? UIButton {
if btn == buttonTag {
btn.tintColor = color
} else {
btn.tintColor = UIColor.whiteColor()
}
}
}
}
This is the concept, i didn't tested, but maybe just need adjust to search inside your scroll view or similar.
But with this will be work nice i believe :D
You could do it like this
for i in 0...5 {
let button = UIButton(type: .Custom)
let x = 50 * i + 10
let y = 50
button.frame = CGRectMake(CGFloat(x), CGFloat(y), 40, 40)
button.setTitle("\(i)", forState: .Normal)
button.tag = i
button.backgroundColor = UIColor.greenColor()
button.tintColor = UIColor.blueColor()
button.addTarget(self, action: "avatarListSelected:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
func avatarListSelected(sender : UIButton){
sender.backgroundColor = UIColor.orangeColor()
for view in self.view.subviews{
if(view.isKindOfClass(UIButton)){
let button = view as! UIButton
if button.tag != sender.tag{
button.backgroundColor = UIColor.greenColor()
}
}
}
}
The frame etc is just for demonstation purpose only, you should of course use your own value. The tintColor property is not valid for all button types. Read the documentation for more information.
I want to, programmatically, add a button to a view and then when the user press this button a second view called "Giraanam" is loaded. I managed to write the code to display the button, but I cannot discover how to make it load the second view... Can anyone please help me?
The code I tried so far is:
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(20, 25, 34, 34)
button.backgroundColor = UIColor.greenColor()
button.setBackgroundImage(UIImage(named:"Back_Resultados.png"),forState: UIControlState.Normal)
button.addTarget(self, action: "Giraanam", forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(button)
You need to implement function Giraanam to create a new view and add to another view. Example:
func Giraanam() {
let newView = UIView()
// customise your view
self.view.addSubview(newView)
}