I created just a blank project in Xcode and added a button. I centered it and set its height and width to 60. Heres my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
var lineView = UIView(frame: CGRect(x: 0, y: 60, width: 5, height: 1))
override func viewDidLoad() {
super.viewDidLoad()
lineView.backgroundColor = UIColor.black
button.addSubview(lineView)
}
#IBAction func sampleButton(_ sender: Any) {
UIView.animate(withDuration: 4.0, animations: {
self.lineView = UIView(frame: CGRect(x: 0, y: 60, width: self.button.frame.width, height: 1))
self.button.addSubview(self.lineView)
}, completion: nil)
}
}
When I click the button I want the width to go from being 5 pixels to the width of the button which is 60. I also want to animate this but I am having trouble. So far when I click the button nothing happens. Would anyone be able to help me out?
Don't create a new view. Just change the frame of the one you already have:
UIView.animate(withDuration: 4.0, animations: {
self.lineView.frame = CGRect(x: 0, y: 60, width: self.button.frame.width, height: 1)
}, completion: nil)
Related
Try to change scrollview height, but it doesn't work programatically
#IBOutlet weak var containerView : UIScrollView!
public override func viewDidAppear(_ animated: Bool) {
self.containerView.frame = CGRect(x: 0, y: 0, width: containerView.frame.width, height: UIScreen.main.bounds.height / 2)
}
I recently started developing in Swift (normally embedded C developer).
I want to create some button (later more than one) programmatically and change its label (just for practice).
For this I created a button class, which contains the button init and the callback function. My problem is that it seems like the #selector is not pointing to the instance of the button class the way I expected it will, so a button click does nothing. Can you tell me what I am doing wrong?
#objc class buttontest : NSObject{
let button = NSButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
#objc func printSomething() {
print("Hello")
self.button.title="TEST13"
}
func buttoninit() -> NSButton{
self.button.title="Test"
self.button.bezelStyle=NSButton.BezelStyle.rounded
self.button.target=self;
//button.action = Selector(ViewController.printSomething)
self.button.action = #selector(self.printSomething)
return self.button
}
}
class ViewController: NSViewController {
private lazy var redBox = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(redBox)
redBox.wantsLayer = true
redBox.layer?.backgroundColor = NSColor.red.cgColor
//button.init("Test12",self,Selector(printSomething())
let button = buttontest()
self.view.addSubview(button.buttoninit())
//self.view.addSubview(buttontest().buttoninit())
// Do any additional setup after loading the view.
}
override func loadView() {
self.view = NSView(frame: NSRect(x: 0, y: 0, width: NSScreen.main?.frame.width ?? 100, height: NSScreen.main?.frame.height ?? 100))
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
The OK version:
#objc class buttontest : NSObject{
let button = NSButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
#objc func printSomething() {
print("Hello")
self.button.title="TEST13"
}
func buttoninit() -> NSButton{
self.button.title="Test"
self.button.bezelStyle=NSButton.BezelStyle.rounded
self.button.target=buttonX
self.button.action = #selector(buttontest.printSomething)
return self.button
}
}
let buttonX = buttontest()
class ViewController: NSViewController {
private lazy var redBox = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(redBox)
redBox.wantsLayer = true
redBox.layer?.backgroundColor = NSColor.red.cgColor
self.view.addSubview(buttonX.buttoninit())
}
}
Does anyone know how I can move a back button image to the edge of the screen?
This is what it looks like right now:
and this is how I want it to look:
If you add custom view as a button and add as leftBarButtonItem it always takes default x (frame's start position) position for that view as defined for navigation bar item. You can add something like this to get your desired output:
class AnotherViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addBackButton()
}
func addBackButton() {
let containerView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 50)))
let btnBack = UIButton(frame: CGRect(x: -25, y: 0, width: 45, height: 45))
btnBack.setImage(UIImage(named: "dark_ic_back.png")?.withRenderingMode(.alwaysTemplate), for: .normal)
btnBack.tintColor = .black
btnBack.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
containerView.addSubview(btnBack)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: containerView)
}
#objc func backAction(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
}
Output:
Try to change its insets:
self.navigationController?.navigationBar.backItem?.backBarButtonItem?.imageInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 12)
and find values to move it right to the edge.
My swift code is all code no storyboard. My slider sx should change the width of the imagview when the value is changed. That is not happening no change is visible on the imageview. The imageview should increase / decrease when the slider value is changed.
import UIKit
class ViewController: UIViewController {
var sx = UISlider()
var pic = UIImageView()
var ww = 80
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[sx,pic].forEach{
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
$0.backgroundColor = UIColor.systemPink
}
pic.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: CGFloat(ww), height: 50)
sx.frame = CGRect(x: view.center.x-115, y: view.center.y + 200, width: 80, height: 50)
sx.addTarget(self, action: #selector(ji), for: .valueChanged)
}
#objc func ji(sender : UISlider){
ww = Int(sx.value)
}
}
Reset frame inside ji function as code inside viewDidLoad won't be triggered again it's called only once when the vc is loaded
#objc func ji(sender : UISlider){
ww = Int(sx.value)! * 80
pic.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: CGFloat(ww), height: 50)
}
I try to create a scrollview. Created a scrollview via Xib, programmatically added scrolledView (a NSView that contains all subview in scrollView).
The following code shows the subviews but the scrollView not scroll. Why?
class LevelScrollController: NSViewController {
#IBOutlet var scrollView: NSScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let scrolledView = NSView(frame: NSRect(x: 0, y: 0, width: scrollView.frame.size.width, height: 300))
// Inserisco pulsanti di esempio
for i in 1 ... 10{
scrolledView.addSubview(NSButton(frame: NSRect(x: 0, y: i*30, width: 100, height: 30)))
}
scrollView.addSubview(scrolledView)
}
Putting the code in viewDidLayout instead of viewDidLoad not change the result: not scroll
Instead of add scrolledView to subview of scrollView, set it as documentView.
Replace scrollView.addSubview(scrolledView) with scrollView.documentView = scrolledView