I need some help over here. I try to learn... some things and practice but i'm not able to manage that function.
I have this function creating the container view:
func createGameView() {
let containerView = UIView()
containerView.frame = CGRect(x: 0,
y: self.screenHeight * 0.4,
width: screenWidth,
height: screenHeight * 0.6)
containerView.backgroundColor = UIColor(red:0.99, green:0.13, blue:0.00, alpha:1)
self.view.addSubview(containerView)
After that I have a function who will populate the container view with 9 squares
func createGridView( _ container : UIView) {
let buttonWidth = container.frame.size.width / 3
let buttonHeight = container.frame.size.height / 3
for i in [0,1,2] {
for j in 0...2 {
let button = UIButton(frame:
CGRect( x : buttonWidth * CGFloat (i),
y :buttonHeight * CGFloat (j),
width : buttonWidth,
height : buttonHeight ))
button.backgroundColor = UIColor.init(colorLiteralRed: 0.1, green: 0.89, blue: 0.05, alpha: 1)
button.layer.borderColor = UIColor.black.cgColor
button.layer.borderWidth = 1.0
container.addSubview(button)
button.addTarget(self, action: #selector(GameViewController.addGameValue(sender:)), for: .touchUpInside)
button.tag = i + 3 * j
self.gameButtons.append(button)
}
}
}
And I have a button called RESET
var resetButton = UIButton(frame:
CGRect(x: Double(containerView.frame.size.width - 100),
y: Double(containerView.frame.size.height - 60),
width: 60,
height: 40.0)
)
resetButton.setTitle ("Reset", for: UIControlState.normal)
resetButton.setTitleColor(UIColor.black, for: UIControlState.normal)
resetButton.setTitleColor(UIColor.white, for: .highlighted)
resetButton.layer.borderWidth = 1.0
resetButton.layer.borderColor = UIColor.black.cgColor
resetButton.layer.cornerRadius = 5.0
resetButton.addTarget(self, action: #selector(GameViewController.resetGame), for: UIControlEvents.touchUpInside)
containerView.addSubview(resetButton)
This is where i got a problem :(
func resetGame (sender: UIButton) {
// .addTarget(self, action: #selector(self.createGameView), for: .touchUpInside)
print(" reset me ")
// .addTarget(self, action: #selector(GameViewController.resetGame), for: .touchUpInside)
}
Anyone can tell me why none of those are not working to reset the value ? Is a tic tac toe game, and after put some x and 0 i want to restore the gridView to intial state. Got no ideea how to do it.
you already have all of the buttons that you need added to the view after createGridView is called, so on reset all you have to do is reset the button state, clear any scores/points and your ready to start again.
func resetGame() {
// reset any scores, colors etc
for button in self.gameButtons {
button.setTitle ("", for: UIControlState.normal)
button.backgroundColor = UIColor.init(colorLiteralRed: 0.1, green: 0.89, blue: 0.05, alpha: 1)
}
}
Related
How do i set the back button x position from the starting point of device position ?
static func backButton(navigationItem: UINavigationItem) {
//Back Button
let btnBack = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 22))
btnBack.setImage(UIImage(named:"icn-back"), for: .normal)
btnBack.addTarget(self, action: #selector(NavBar.backBtn), for: .touchUpInside)
let leftBtn = UIBarButtonItem.init(customView: btnBack)
leftBtn.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setLeftBarButton(leftBtn, animated: false)
}
let leftView = UIView(frame: CGRect(x: desiredXValue, y: 0, width: 90, height: 21))
let leftButton = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 10))
leftButton.setImage(UIImage(named: "Group.png"), for: UIControlState())
leftButton.addTarget(self, action: #selector(ViewController.action(_:)), for: UIControlEvents.touchUpInside)
leftButton.contentMode = .scaleAspectFit
leftView.addSubview(leftButton)
leftView.contentMode = .scaleAspectFit
let leftBarButton = UIBarButtonItem(customView: leftButton)
self.navItem.leftBarButtonItem = leftBarButton
UserDefaults.standard.synchronize()
I have this following header for my table view header section
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = .white
// add a button
let button = UIButton(type : .system)
button.showsTouchWhenHighlighted = false
button.frame = CGRect(x: 0 , y: 0 , width: 20 , height: 20 )
if(collapsed[section])
{ button.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)}
else
{ button.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)}
button.addTarget(self, action: #selector(reloadTable), for: .touchUpInside)
button.tag = section
view.addSubview(button)
button.bindFrameToSuperviewBounds()
// add an Image
let image = UIImageView(image: UIImage(named: "sup1"))
image.frame = CGRect(x: 150 , y: 5 , width: 100 , height: 100)
image.contentMode = .scaleAspectFit
view.addSubview(image)
// add a label
let lblNew = UILabel()
lblNew.frame = CGRect(x: 20, y: 100 , width: 100, height: 30)
lblNew.text = "١٢٣ ر.س"
lblNew.textColor = UIColor.myDarkGreen
lblNew.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(lblNew)
// view.dropShadow()
return view
}
Now I want to add drop shadow effect on the section header to look like this
I have tried some solutions here but it works on simple view that is not used as a viewForHeaderInSection, any tips?
Use the view's layer's shadow properties:
view.layer.shadowOpacity = 1
view.layer.shadowOffset = CGSize(width: 0, height: 1)
view.layer.shadowRadius = 1
Result:
This question already has answers here:
Add view over tableview (UITableViewController)
(7 answers)
Closed 5 years ago.
I have a table view controller and the only way for me is to using that so please don't tell me use viewController instead of tableviewController here is my codes for adding a button and this Button is shown in the last cell But I want when user scrolling table view the button be at the bottom of screen without moving
override func viewDidLoad() {
super.viewDidLoad()
let submitButton = UIButton(frame: CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50))
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
view.addSubview(submitButton)
Try this approach if it suits your scenario:
var submitButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
submitButton.frame = CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50)
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
self.view.window?.addSubview(submitButton)
}
But in this scenario you have to remove the button from superview in viewWillDisappear :
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self. submitButton.removeFromSuperview()
}
Try this
declare button as a variable
var submitButton:UIButton!
and in viewDidLoad
submitButton = UIButton(frame: CGRect(x: 5, y: UIScreen.main.bounds.size.height - 50 , width: UIScreen.main.bounds.size.width - 10 , height: 50))
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
self.view.addSubview(submitButton)
and implement this method
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
submitButton.frame = CGRect.init(x: submitButton.frame.origin.x, y: UIScreen.main.bounds.size.height + scrollView.contentOffset.y - 50 , width: submitButton.frame.width, height: submitButton.frame.height)
}
I am trying to put multiple buttons in a scrollView but it just scrolls background view.
class HomeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Home"
let scrollView = UIScrollView()
let view = UIView()
scrollView.frame = self.view.bounds
self.view.backgroundColor = .green
scrollView.backgroundColor = .blue
scrollView.addSubview(view)
self.view.addSubview(scrollView)
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height * 3)
view.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height)
view.backgroundColor = .yellow
attractivePlaceButtonSetup()
eatDrinkButtonSetup()
ShoppingButtonSetup()
festivalEventButtonSetup()
hotelGuestHouseButtonSetup()
travellerEssentialButtonSetup()
dealButtonSetup()
seeDoButtonSetup()
}
I wrote this code for button frame
func eatDrinkButtonSetup(){
let button = UIButton()
button.frame = CGRect(x: 5, y: 225, width: self.view.frame.size.width - 10, height: 150)
button.setTitle("Eat & Drink", for: .normal)
button.setBackgroundImage(#imageLiteral(resourceName: "imageName"), for: .normal)
button.titleEdgeInsets = UIEdgeInsets(top: -120, left: -200, bottom: 0, right: 0)
button.addTarget(self, action: #selector(targetEatDrink), for: .touchUpInside)
view.addSubview(button)
}
}
I also try to such way but it just scroll a button.
scrollView.addSubview(attractivePlaceButtonSetup)
self.view.addSubview(scrollView)
//Try this...
//Background Scroll Creation
var stickersScrollViewCount = 0
func stickersScrollContents() {
var xCoord: CGFloat = 5
let yCoord: CGFloat = 5
let buttonWidth:CGFloat = 45.0
let buttonHeight: CGFloat = 45.0
let gapBetweenButtons: CGFloat = 5
for i in 0..<stickersImageArray.count{
stickersScrollViewCount = i
// Button properties
let filterButton = UIButton(type: .custom)
filterButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
filterButton.tag = stickersScrollViewCount
filterButton.backgroundColor = UIColor.clear
filterButton.setTitleColor(UIColor.white, for: .normal)
filterButton.titleLabel?.adjustsFontSizeToFitWidth = true
filterButton.showsTouchWhenHighlighted = true
let myimage = UIImage(named: stickersImageArray[stickersScrollViewCount])
filterButton.setImage(myimage, for: .normal)
filterButton.addTarget(self, action:#selector(StickersActionTapped), for: .touchUpInside)
filterButton.layer.cornerRadius = 5
filterButton.clipsToBounds = true
xCoord += buttonWidth + gapBetweenButtons
bgScrollView.addSubview(filterButton)
}
bgScrollView.contentSize = CGSize(width: buttonWidth * CGFloat(stickersScrollViewCount+2), height: yCoord)
}
//Call the function where ever you want viewDidLoad() or on Button Click!!
//Hope this helps!!!
I am creating circular buttons as follow:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = GradientButton.createCircularButton(20, yPos: 20, width: 30, height: 30, circleValue:20)
button.addTarget(self, action: #selector(ViewController.didCircleBtnTouched(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
}
enum ColorType {
case red, green, orange
}
class GradientButton: UIButton {
var colorType: ColorType?
public class func createCircularButton(xPos: CGFloat, yPos: CGFloat, width: CGFloat, height: CGFloat, circleValue: Int) -> GradientButton {
let button = GradientButton()
button.titleLabel!.font = UIFont(name:"HelveticaNeue", size: 12)
let buttonFrame = CGRect(x: xPos, y: yPos, width: width, height: height)
button.frame = buttonFrame
button.backgroundColor = UIColor.clearColor()
button.backgroundColor = UIColor.whiteColor()
button.layer.borderWidth = 1
button.layer.cornerRadius = 15.0
//this helps making it circular not rectangle
button.clipsToBounds = true
let red = UIColor(red:134/255, green:16/255, blue:1/255, alpha:1.0)//red
let green = UIColor(red:0/255, green:136/255, blue:43/255, alpha:1.0)//green
let orange = UIColor(red:243/255, green:144/255, blue:25/255, alpha:1.0)//orange
if(circleValue <= 3){
button.colorType = .green
button.layer.borderColor = UIColor.greenColor().CGColor
button.setTitleColor(green, forState: .Normal)
} else if(circleValue > 3 && circleValue <= 7){
button.colorType = .orange
button.layer.borderColor = UIColor.orangeColor().CGColor
button.setTitleColor(orange, forState: .Normal)
} else if(circleValue > 7){
button.colorType = .red
button.layer.borderColor = UIColor.redColor().CGColor
button.setTitleColor(red, forState: .Normal)
}
button.setTitle("\(circleValue)", forState: .Normal)
return button
}
}
And on click of button I am applying gradient as follow:
func didCircleBtnTouched(sender:GradientButton!){
ApplyGradientToButton(sender)
}
func ApplyGradientToButton(sender: GradientButton!){
var color1 = UIColor()
var color2 = UIColor()
if(sender.colorType == .red){
color1 = UIColor(red:134/255, green:16/255, blue:1/255, alpha:1.0)
color2 = UIColor(red:200/255, green:37/255, blue:6/255, alpha:1.0)
}
else if(sender.colorType == .green) {
color1 = UIColor(red:0/255, green:136/255, blue:43/255, alpha:1.0)
color2 = UIColor(red:112/255, green:191/255, blue:65/255, alpha:1.0)
}
else if(sender.colorType == .orange) {
color1 = UIColor(red:200/255, green:110/255, blue:1/255, alpha:1.0)
color2 = UIColor(red:239/255, green:149/255, blue:26/255, alpha:1.0)
}
sender.setTitleColor(UIColor.whiteColor(), forState: .Normal)
var layer = sender.layer
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowOffset = CGSize(width: 0.0, height: 5.0)
layer.shadowOpacity = 1.0
layer.shadowRadius = 10.0
sender.applyGradient([color2, color1], locations: [0.0, 0.90])
}
I need to display shadow around circular button on button click. But shadow is not getting displayed.
I think button.clipsToBounds = true is responsible as when I am not using this property shadow appears. Unfortunately by removing this property my circular button becomes rectangular after click which is not desirable.
Is there any way to display the shadow without changing the shape of button?
Please advise?
Current output:
Expected output:
Well you can't clip/mask to bounds and add a shadow, as it will also be clipped or masked too.
The solution is to add the shadow on a separate layer and add the image as a sublayer.
This is described in many posts here:
https://stackoverflow.com/a/25591916/312312
Swift - Problems with corner radius and drop shadow
and many many more
Just Put this code in viewController.swift file.. (Or any other file)
extension UIView {
func setRadiusWithShadow(_ radius: CGFloat? = nil) {
self.layer.cornerRadius = radius ?? self.frame.width / 2
self.layer.shadowColor = UIColor.darkGray.cgColor
self.layer.shadowOffset = CGSize(width: 1.5, height: 1.5)
self.layer.shadowRadius = 1.0
self.layer.shadowOpacity = 0.7
self.layer.masksToBounds = false
}
}
and then apply to your button..
btnEdit.setRadiusWithShadow()
Yeah... Just see the output... Everything is done...
Note: You can change CGSize(width: 1.5, height: 1.5) values as per your desired need...