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:
Related
I’m new to swift and coding in general and I’m trying to make a truth or dare game but when I run the code using step through my code and press the red button it is running the code from Responder and not from responder1. I think the problem might be that I maid the 2nd button just by copying the for the 1st button and adding 1, and then swift thinks that the button 1 is the same as button and it runs the code for the 1st button.
import PlaygroundSupport
import UIKit
import SpriteKit
//
let view = UIView()
view.backgroundColor = #colorLiteral(red: 0.0, green: 0.7522757053375244, blue: 0.780606746673584, alpha: 1.0)
view.frame = CGRect(x: 0, y: 0, width: 400, height: 300)
let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
lbl.text = "Hello, World!"
view.addSubview(lbl)
lbl.textColor = .blue
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 100, height: 50))
button.backgroundColor = #colorLiteral(red: 0.721568644046783, green: 0.886274516582489, blue: 0.592156887054443, alpha: 1.0)
//button.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected)
button.setTitle("button", for: UIControl.State.selected)
button.layer.cornerRadius = 10
view.addSubview(button)
let button1 = UIButton(frame: CGRect(x: 250, y: 100, width: 100, height: 50))
button1.backgroundColor = #colorLiteral(red: 1.0, green: 0.1893465518951416, blue: 0.1893465518951416, alpha: 1.0)
//button1.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected)
button1.setTitle("button1", for: UIControl.State.selected)
button1.layer.cornerRadius = 10
view.addSubview(button1)
class Responser: NSObject
{
//Method to be called
#objc func printname()
{
lbl.text = "aha"
}
}
let responder = Responser()
button.addTarget(responder, action: #selector(Responser.printname), for:.touchUpInside)
class Responser1: NSObject
{
//Method to be called
#objc func printname()
{
lbl.text = "UAh"
}
}
let responder1 = Responser()
button1.addTarget(responder1, action: #selector(Responser1.printname), for:.touchUpInside)
PlaygroundPage.current.liveView = view
I am trying to add a section title Label and a button in the header section view. but it looks empty. when I run the application the header is empty. the 2nd section code work fine
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if (section == 0){
let label = UILabel.init(frame: CGRect.init(x: 17, y: 139, width: tableView.frame.size.width, height: 45))
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize: 13.0)
label.textAlignment = .left
label.text = " My Balances"
label.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
let frame = tableView.frame
let height:CGFloat = 66
let button = UIButton(frame: CGRect(x: 306, y: 139, width: 15, height: 15)) // create button
button.tag = section
// the button is image - set image
button.setImage(UIImage(named: "remove_button"), for: .normal)
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: height)) // create custom view
headerView.addSubview(button) // add the button to the view
headerView.addSubview(label)
return headerView
//return label
//return label
}
else {
let label = UILabel.init(frame: CGRect.init(x: 0, y: 241, width: tableView.frame.size.width, height: 45))
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize: 13.0)
label.textAlignment = .left
label.text = " My Customers"
label.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
return label
}
}
You are not following the proper way. First you have to set height of header view using heightForHeaderInSection from tableview object in viewDidLoad() like -
tableView.heightForHeaderInSection = 250
or by using its delegate method -
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 250
}
You set the height of header view equal to tableview height. Set it less than it let height:CGFloat = 250 -
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel.init(frame: CGRect.init(x: 0, y: 241, width: tableView.frame.size.width, height: 45))
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize: 13.0)
label.textAlignment = .left
label.text = " My Balances"
label.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
let frame = tableView.frame
let height:CGFloat = 250
let button = UIButton(frame: CGRect(x: 5, y: 10, width: 15, height: 15)) // create button
button.tag = section
// the button is image - set image
button.setImage(UIImage(named: "remove_button"), for: .normal)
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: height)) // create custom view
headerView.addSubview(button) // add the button to the view
headerView.addSubview(label)
return headerView
//return label
}
Or another way is to make custom reusable header view, register as header view and finally dequeu it.
You can follow the documentation from apple for second way - https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections
I have a tableView inside a navigationController inside a tabBarController.
I want to programatically fix a view above the tabbar like this:
.
I have experimented with adding constraints without success, also because I don't know whether to use self.view or self.bottomLayoutGuide of self.tableView or self.navigationController or self.tabBarController.tabBar or ... any suggestions?
Try this:
button = UIButton(frame: CGRect(x: 0, y: self.view.frame.height - 99, width: self.view.frame.width, height: 50))
button.backgroundColor = UIColor(red: 0, green: 88/255, blue: 171/255, alpha: 1)
button.tintColor = .white
button.titleLabel?.font = UIFont(name: "Helvetica", size: 20)
button.setTitle("YOUR TEXT", for: .normal)
button.addTarget(self, action: #selector(yourAction), for: .touchUpInside)
self.navigationController?.view.addSubview(button)
In my example, it is a UIButton but it can be a UIView too
Same example with UIView
let view = UIView(frame: CGRect(x: 0, y: self.view.frame.height - 99, width: self.view.frame.width, height: 50))
view.backgroundColor = UIColor(red: 0, green: 88/255, blue: 171/255, alpha: 1)
self.navigationController?.view.addSubview(view)
self.automaticallyAdjustsScrollViewInsets = NO;
then you will see the tableviewcell covered by navigationbar and
if you want navigationbar to be full transparency
self.navigationController.navigationBar.backgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault;
self.navigationController.navigationBar.shadowImage:[UIImage new];
Try this Swift 3.0
Bottom view code here.
let bottomview = UIView(frame: CGRect(x: 0, y: self.view.frame.height-63,width:self.view.frame.width, height: 80))
bottomview.backgroundColor = UIColor.red
self.view.addSubview(bottomview)
Table view code here...
let table: UITableViewController = MyTableViewController()
let tableView: UITableView = UITableView()
tableView.frame = CGRect(x: 0, y:self.navigationController.navigationBar.frame.size.height, width: self.view.frame.width, height: 500)
tableView.dataSource = table
tableView.delegate = table
self.view.addSubview(tableView)
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)
}
}
I have a system that generate new view (marked with blue rect on the picture) and button (marked with green circle ) as a subview of Superview(marked with red rect) when user tapped to another button(named as Button). They all created programmatically. I can remove the "minus button" according it's button.tag with Action Messages. The problem is that how do I remove that subview next to minus button? It has also same tag with minus button.
My mainButton IBAction below ;
#IBAction func mainButton(sender: UIButton) {
drawThem()
minusButtonY = counter == 0 ? (minusButtonY + 101) :(minusButtonY + 68)
borderY = counter == 0 ? (borderY + 86) : (borderY + 68)
counter++
drawFooter()
completePaymentView.contentSize = counter > 7 ? (CGSizeMake(500.0, ( borderY ))) : (CGSizeMake(500.0, ( 490.0 )))
}
And the the method where I generate new view and button ;
func drawThem() {
//creating minus button
let image = UIImage(named: "minus.png") as UIImage?
let button = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
var mX : CGFloat = 2.0
var mY : CGFloat = minusButtonY >= 36.0 ? minusButtonY : 36.0
button.frame = CGRectMake(mX, mY, 22, 22)
button.setImage(image, forState: .Normal)
button.tag = Int(counter)
button.addTarget(self, action:"btnTouched:", forControlEvents:.TouchUpInside)
completePaymentView.addSubview(button)
//drawing payment section border
var x : CGFloat = 43.0
var y : CGFloat = borderY >= 20.0 ? borderY : 20.0
let size = CGSize(width: 485 , height: 55)
let borderView = UIView(frame: CGRect(origin: CGPoint(x: x , y: y), size: size))
borderView.layer.borderColor = UIColor(red: 0.83, green: 0.85, blue: 0.88, alpha: 1.0).CGColor
borderView.layer.borderWidth = 2.0
borderView.layer.cornerRadius = 5.0
borderView.tag = counter
completePaymentView.addSubview(borderView)
//drawing UIImage and Labels into border
let imageName = "circle.png"
let thumb = UIImage(named: imageName)
let imageView = UIImageView(image: thumb!)
imageView.frame = CGRect(x: 14, y: 8, width: 55, height: 35)
borderView.addSubview(imageView)
let label1 = UILabel(frame: CGRect(x: 85, y: 20, width: 100, height: 18)) as UILabel
label1.text = "Some Text"
label1.textColor = UIColor(red: 0.49, green: 0.54, blue: 0.6, alpha: 1.0)
label1.font = label1.font.fontWithSize(14.0)
label1.textAlignment = .Left
borderView .addSubview(label1)
let label2 = UILabel(frame: CGRect(x: 370, y: 20, width: 100, height: 18)) as UILabel
label2.text = "Some Text"
label2.textColor = UIColor(red: 0.49, green: 0.54, blue: 0.6, alpha: 1.0)
label2.font = label2.font.fontWithSize(14.0)
label2.textAlignment = .Right
borderView .addSubview(label2)
}
and Target Action below ;
func btnTouched(button : UIButton){
button.removeFromSuperview()
}
Add the following in btnTouched method. Actually I am searching through all the subviews to find the tag.
for view in self.view.subviews as! [UIView] {
if view.tag == button.tag {
view.removeFromSuperview()
}
}