How to add subview so that it overlaps another view? - ios

I have the following subview
webView = WKWebView(frame: CGRect(x: 3, y: horizontalLine.frame.maxY + 8, width: self.view.frame.width-6, height: CGFloat(self.view.frame.height - 150)))
webView.navigationDelegate = self
self.view.addSubview(webView)
I want to add the following subview on top of it (not inside it).
textSizeChangeView = UIView(frame: CGRect(x: 0, y: textBtn.frame.maxY, width: self.view.frame.width, height: 100))
textSizeChangeView.backgroundColor = UIColor(red: 0.97, green: 0.98, blue: 0.98, alpha: 1.00)
let mySlider = UISlider(frame:CGRect(x: 10, y: 0, width: self.view.frame.width - 20, height: 30))
self.textSizeChangeView.addSubview(mySlider)
mySlider.center = self.textSizeChangeView.center
mySlider.minimumValue = 1
mySlider.maximumValue = 2
mySlider.isContinuous = true
mySlider.tintColor = UIColor(red: 0.33, green: 0.79, blue: 0.93, alpha: 1.00)
textSizeChangeView.isHidden = true
mySlider.addTarget(self, action: #selector(self.sliderValueChanged(sender:)), for: .valueChanged)
self.view.addSubview(self.textSizeChangeView)
How can I do this?

The order of adding subviews matters.
If you want to make sure that textSizeChangeView will be on top of webView in case of overlapping then you need to make sure that self.view.addSubview(webView) is called before self.view.addSubview(self.textSizeChangeView).

Related

I’m trying to make two buttons that print two different things in swift playground

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

UITextField - bottom line programmatically

I am trying to add a bottom line to UITextField ...
what am I doing wrong?
let emialTextField: UITextField = {
let textField = UITextField()
textField.layer.cornerRadius = 5
textField.borderStyle = .none
textField.placeholder = "emial adress"
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0, y: textField.frame.height - 2, width: textField.frame.width, height: 2)
bottomline.backgroundColor = UIColor.init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1).cgColor
textField.layer.addSublayer(bottomline)
return textField
}()
Try below code.
override func viewDidLoad() {
super.viewDidLoad()
let emialTextField: UITextField = {
let textField = UITextField(frame: CGRect(x: 100, y: 90, width: 100, height: 30))
textField.borderStyle = .none
textField.placeholder = "emial adress"
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0, y: textField.frame.height + 1, width: textField.frame.width, height: 2)
bottomline.backgroundColor = UIColor.init(red: 3/255, green: 4/255, blue: 5/255, alpha: 1).cgColor
textField.layer.addSublayer(bottomline)
return textField
}()
view.addSubview(emialTextField)
emialTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 100).isActive = true
emialTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 90).isActive = true
emialTextField.heightAnchor.constraint(equalToConstant: 30).isActive = true
emialTextField.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
It seems textField.frame.width is zero when executing below line of your code:
bottomline.frame = CGRect(x: 0, y: textField.frame.height - 2, width: textField.frame.width, height: 2)
So you should add bottomline layer to your text field after size set for emialTextField via constraint or frame size.

How to Add label On the top of the View Swift

Here is what I am trying to do:
The screenshot is taken from iPhone:
Code Below:
//Show HighLightView
let highLightView = UIView(frame: CGRect(x: self.minXHighLightView!, y: 0, width: self.maxXHighLightView!-self.minXHighLightView!, height: self.soundWaveView.frame.height))
highLightView.backgroundColor = UIColor(displayP3Red: 89/256, green: 206/256, blue: 249/256, alpha: 0.4)
self.soundWaveView.addSubview(highLightView)
//Show HighLight Lable
let highLightLabelCount = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
highLightLabelCount.textColor = .black
highLightLabelCount.font = UIFont(name: "Lato-Bold", size: 20)
highLightView.addSubview(highLightLabelCount)
Question: How to set Frame or constraints label setup at top Left Corner?
Any help would be greatly appreciated.
Thanks in advance.
Your constraint should be centered x,y not top left with
//Show HighLightView
let highLightView = UIView(frame: CGRect(x: self.minXHighLightView!, y: 0, width: self.maxXHighLightView!-self.minXHighLightView!, height: self.soundWaveView.frame.height))
highLightView.backgroundColor = UIColor(displayP3Red: 89/256, green: 206/256, blue: 249/256, alpha: 0.4)
self.soundWaveView.addSubview(highLightView)
//Show HighLight Lable
let highLightLabelCount = UILabel()
highLightLabelCount.textColor = .black
highLightLabelCount.font = UIFont(name: "Lato-Bold", size: 20)
highLightView.addSubview(highLightLabelCount)
highLightLabelCount.translatesAutoresizingMaskIntoConstraints = false
self.highLightView.textAlignment = .center
NSLayoutConstraint.activate([
self.highLightLabelCount.centerXAnchor.constraint(equalTo: self.highLightView.centerXAnchor),
self.highLightLabelCount.centerYAnchor.constraint(equalTo: self.highLightView.centerYAnchor)
])
I think this will be useful if you would like to pin label to top left corner of your highlight view.
NSLayoutConstraint.activate([
highLightView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
highLightView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
highLightLabelCount.leadingAnchor.constraint(equalTo: view.leadingAnchor),
highLightLabelCount.bottomAnchor.constraint(equalTo: highLightView.topAnchor))
])

How to programatically fix a view to a tab bar in Swift?

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)

Creating a button in the Playground app on iPad

I am working with the new beta version of the Playground app in iOS 10.
I want to get some UIKit elements working in a playground.
Swift 3 is just different enough from Swift 2, I am having some problems.
I got a label and text box working, but I cannot get a button working.
I have the button showing up but cannot get text in it or an action working on touch-up.
Here is my code I have.
import PlaygroundSupport
import UIKit
//
let view = UIView()
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)
PlaygroundPage.current.liveView = view
let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
lbl.text = "Hello, World!"
view.addSubview(lbl)
let txt = UITextField(frame: CGRect(x: 150, y: 200, width: 200, height: 50))
//txt.placeholder = "Enter text here"
//txt.font = UIFont.systemFont(ofSize: 15)
txt.borderStyle = UITextBorderStyle.roundedRect
view.addSubview(txt)
func buttonPressed(sender: UIButton)
{
//sender.backgroundColor = #colorLiteral(red: 0.725490212440491, green: 0.47843137383461, blue: 0.0980392172932625, alpha: 1.0)
}
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.focused)
button.setTitle("button", for: UIControlState.focused)
button.layer.cornerRadius = 10
button.addTarget(button, action: "buttonPressed", for: UIControlEvents.touchUpInside)
view.addSubview(button)
I've been trying the same and the following code works for me:
import UIKit
import PlaygroundSupport
class Receiver {
#objc func buttonClicked() {
print("click")
}
}
let view = UIView()
view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
let receiver = Receiver()
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 50))
button.setTitle("TestButton", for: .normal)
button.setTitleColor(#colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0), for: .normal)
button.addTarget(receiver, action: #selector(Receiver.buttonClicked), for: .touchUpInside)
PlaygroundPage.current.liveView = view
view.addSubview(button)
I think the important difference is that you add the button as target of the .touchUpInside event. Unfortunately, the button does not implement your buttonPressed method (it's just defined in the Playground, not as part of the UIButton class). As there is no self in a general playground (to refer to the function you wrote), I wrapped it into a new class and used an instance of this class to be the receiver of the .touchUpInside event.

Resources