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

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)

Related

view.addSubview(: ) Not working in swift playgrounds

I've got a swift playground and I've created a UIButton called 'startButton'.
I have a function that sets up the parameters of the UIButton.
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonPressed), for: .touchUpInside)
view.addSubview(startButton)
}
I call this in the viewDidLoad
override func viewDidLoad() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 524, height: 766))
view.backgroundColor = .white
setupStartButton()
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
}
When I run the playground it doesn't show the button.
How do I show the button.
Thank you
You are setting PlaygroundPage.current.liveView to the local view created in viewDidLoad. This is not the same view of the view controller which is what you are adding the button to in the setupStartButton.
I would remove the line let view = UIView(frame: CGRect(x: 0, y: 0, width: 524, height: 766)) then every reference to view will be the view controller's view.
You should remove the playground code from the viewDidLoad method. After the view controller class, you can create an instance of the view controller and setup the playground:
Something like:
class MyViewController: UIViewController {
// your code
}
PlaygroundPage.current.liveView = MyViewController()

Two UIButton form one having gradient color and another having border and rounded corner

I am trying to achive this using gradient color and corner radius on [.topLeft,.bottomLeft] and [.topRight,.bottomRight] but I am not able to achieve the border color on one side.
Here is my code
//btnMale gradient color
btnMale.roundCorners([.topLeft,.bottomLeft], radius: 20)
btnFemale.roundCorners([.topRight,.bottomRight], radius: 20)
btnMale.backgroundColor = .clear
let gradient1: CAGradientLayer = CAGradientLayer()
gradient1.frame = CGRect(x: 0, y: 0, width: (self.view.frame.size.width - 90)/2 , height: btnMale.frame.size.height)
gradient1.colors = [Colors.appYellow,Colors.appRed].map { $0.cgColor }
gradient1.startPoint = GradientOrientation.horizontal.startPoint
gradient1.endPoint = GradientOrientation.horizontal.endPoint
btnMale.layer.insertSublayer(gradient1, at: 0)
btnMale.applyGradient(withColours: [Colors.appYellow,Colors.appRed], gradientOrientation: .horizontal)
btnMale.borderWidth = 0.0
btnFemale.borderWidth = 0.5
btnFemale.borderColor = .black
btnMale.setImage(UIImage(named: Images.imgMaleIconWhite), for: .normal)
btnFemale.setImage(UIImage(named: Images.imgFemaleIconBlack), for: .normal)
and here is my output
Please help.
Pradip it's better to add buttons in UIView. It will be easy for you to manage. I made UI like you want using UIView. See this,
let button = UIButton.init(frame: CGRect.init(x: 0, y: 250, width: 200, height: 100))
button.setTitle("HI", for: .normal)
button.addTarget(self, action: #selector(self.btnTapped(pageNo:)), for: .touchUpInside)
self.view.addSubview(button)
let view = UIView.init(frame: CGRect.init(x: 50, y: 250, width: 250, height: 50))
view.backgroundColor = UIColor.red
self.view.addSubview(view)
let btnMale = UIButton.init(type: .custom)
btnMale.frame = CGRect.init(x: 0, y: 0, width: 125, height: 50)
btnMale.setTitle("Male", for: .normal)
btnMale.applyGradient(colours: [UIColor.yellow,UIColor.red])
view.addSubview(btnMale)
let btnFemale = UIButton.init(type: .custom)
btnFemale.frame = CGRect.init(x: 125, y: 0, width: 125, height: 50)
btnFemale.setTitle("Female", for: .normal)
view.addSubview(btnFemale)
view.layer.cornerRadius = 25
view.clipsToBounds = true
Output of above code will look like this.

Adding UITableView backgroundView with UIView with UIButton

I am trying to set my UITableView backgroundView to UIView and inside the UIView I added a UIButton but the UIButton is not appearing. What am I doing wrong?
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.menuTable.bounds.size.width, height: self.menuTable.bounds.size.height))
let loginButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
loginButton.setTitle("Login", for: .normal)
loginButton.backgroundColor = UIColor.white
loginButton.tintColor = UIColor.black
loginButton.addTarget(self, action:#selector(self.pressedConnection), for: .touchUpInside)
view.addSubview(loginButton)
self.menuTable.backgroundView = view
self.menuTable.separatorStyle = .none

Swift Add UIView with subviews to titleView

I'd like to customize my navigation bar and insert a UIView with two subviews into the title view of the navigation bar.
Unfortunately the subviews aren't displayed.
Do you have any idea why?
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
let searchIcon = UIButton()
searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.layer.frame = CGRect(x: 0, y: (UIScreen.main.bounds.size.height - 28) / 2, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width-28, height: UIScreen.main.bounds.size.height)
titleLabel.textColor = UIColor(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(self.titleLabel)
self.navigationItem.titleView = titleContainer
problem and expected solution:
Why did you use height of all the element as UIScreen.main.bounds.size.height? Please try the below code.
P.S. I have changed the color of searchIcon as i didn't have the image and changed the color of text of label and added some text so that it is visible.
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 45)
let searchIcon = UIButton()
//searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.backgroundColor = UIColor.red
searchIcon.layer.frame = CGRect(x: 0, y: 8, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: 8, width: UIScreen.main.bounds.size.width-28, height: 30)
titleLabel.textColor = UIColor.blue//(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(titleLabel)
titleLabel.text = "Sample Text"
self.navigationItem.titleView = titleContainer
Here is the screenshot of the above code:
Please change the x,y accordingly as per your requirement.
Have you considered making your let bar = UINavigationBar() programmatically and then add it with: self.view.addSubview(bar)?
So you can push all the "UIBarItems" you want..?

How to add a static header in JSQMessageView Controller

How i can add header just like the image below which should not scroll up with view controller.
To add static header view, use following function (Swift 4):
Note: I have tried to put this kind of header view directly using storyboard. But, it wont worked. So, finally I achieved it by adding programatically.
func addHeaderView() {
let selectableView = UIView(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 60))
selectableView.backgroundColor = UIColor(red: 56.0/255.0, green: 120.0/255.0, blue: 222.0/255.0, alpha: 1.0)
let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
btn.setImage(UIImage(named:"ArrowBack"), for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
selectableView.addSubview(btn)
let titleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 5, width: selectableView.frame.size.width-60, height: 18))
titleLabel.textColor = UIColor.white
titleLabel.text = "Winter event 2017"
selectableView.addSubview(titleLabel)
let subTitleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 30, width: 100, height: 16))
subTitleLabel.textColor = UIColor.white
subTitleLabel.text = "135 members"
selectableView.addSubview(subTitleLabel)
let btnOptions: UIButton = UIButton(frame: CGRect(x: selectableView.frame.size.width-60, y: 0, width: 50, height: 50))
btnOptions.setImage(UIImage(named:"Settings"), for: .normal)
btnOptions.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnOptions.tag = 2
selectableView.addSubview(btnOptions)
view.addSubview(selectableView)
}
//then make a action method :
#objc func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
self.navigationController?.popViewController(animated: true)
} else {
askForSettings()
}
}
Also put this line of code in viewDidLoad()
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 80, left: 0, bottom: 0, right: 0)

Resources