Can't set var in subview - swift

I have an app with a UIViewController with custom UIViews in it. I want to set a variable in that custom UIView and it doesn't update...
I have done this a thousand times and it always worked but this time it doesn't and I really can't figure out what I'm doing wrong, any help is welcome!
In my viewcontroller:
var sjablonview: sjablonPicker!
sjablonview = sjablonPicker(frame: CGRect(x: btnUndo.frame.origin.x, y: DrawingView.frame.origin.y,
width: (btnX.frame.origin.x + btnX.frame.size.width) - btnUndo.frame.origin.x, height: 250))
sjablonview.alpha = 1
sjablonview.mpUitklapping = 300
self.view.addSubview(sjablonview)
In my custom UIView:
class sjablonPicker: UIView {
var mpUitklapping = CGFloat(100)
override init(frame: CGRect){
super.init(frame: frame)
print(mpUitklapping) // --> output = 100 and not 300
...

You are printing from inside your initialiser. Your view is initialized before you change the value of mpUitklapping. Your initializer gets called from this line:
sjablonview = sjablonPicker(frame: CGRect(x: btnUndo.frame.origin.x, y: DrawingView.frame.origin.y, width: (btnX.frame.origin.x + btnX.frame.size.width) - btnUndo.frame.origin.x, height: 250))
The value gets changed, you just don't print the updated value.

Related

How to make a stretchable header view collection view [Swift]

In my swift app I've a collection view and I want to creare a stretchable header view like this in table view: https://medium.com/if-let-swift-programming/how-to-create-a-stretchable-tableviewheader-in-ios-ee9ed049aba3
You already answered your question yourself with that article link, unless I miss something.
I will copy & paste for you and others that may have the same question, if it helps, because it even ships with a github link (kudos to Abhimuralidharan # Medium):
Create a tableview with the basic datasource and delegate methods which are required to simply load the table with some data.
Set the tableview’s contentInset property:
tableView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0)
Here, I set the top value as 300 which is a calculated number which I will set as the initial normal height for the header imageview. Now, that we set the contentInset , the tableview’s frame will start at (0,0) and the first cell will start at (0,300).
Now, create an imageview with height 300 and add it to the current View above the tableview.
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 300)
imageView.image = UIImage.init(named: “poster”)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
Then, add the following code in the scrollview delegate method scrollViewDidScroll which gets called every time the tableview is scrolled.
func scrollViewDidScroll (_ scrollView: UIScrollView) {
let y = 300 — (scrollView.contentOffset.y + 300)
let height = min(max(y, 60), 400)
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height)
}
Compile and run the code. Full source code is available in github.

Add action to button in .xib file Using Swift(4.0)

I implemented horizontal(Paging) slide show of views, created separate xib and integrated with the view controller. Slide show is working as expected but I want add action to the every views so that from there I can move directly to the respective main content pages. Find the below code for implementation of slide show.
func createSlides() -> [Slide] {
let slide1:Slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide
slide1.labelTitle.text = "Page1"
let slide2:Slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide
slide2.labelTitle.text = "Page2"
return [slide1, slide2]
Slide Function
func setupSlideScrollView(slides : [Slide]) {
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: view.frame.height)
scrollView.isPagingEnabled = true
for i in 0 ..< slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(slides[i])
}
}
Xib file
class Slide: UIView {
#IBOutlet weak var labelTitle: UILabel!
var onClickCallback: (() -> Void)?
override init (frame : CGRect) {
super.init(frame : frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
If I think what you are asking is right then do this. (BTW I am a bit rusty at this so please don't crucify me if I'm wrong). Also I don't know where you are designing this so I am going to assume it is in a different place to main.viewcontroller. If it isn't just add a button and create a segue to the content page.
Make a public var in createSlides Func and set it to the slide you are on. Eg: Slide 1. When slide two is opened it should update the var two slide two. For the sake of this we will call the var test
Add a button into your design file
Add a #IBAction into the viewcontroller. This #IBAction should be linked with your button.
inside this #IBAction create a segue to the content using the variable we created before. self.performSegue(withIdentifier: test, sender: self)
In the main view controller add two segues from the view with the slidshow to the view controller with the content (Assuming you only have two slides). One segue should be called slide 1 (The same name as the let you created in createSlides func.) The second should be called slide 2 (for obvious reasons.)
Now when your button in slide 1 is pressed it should perform the segue slide 1 and show your main content
Hope I helped, Toby.
Also if this doesn't work, is not what you want to achieve or is badly worded please comment what it is and I will try to fix it.
call this function and pass pageIndex where you want to jump:
func jumpToView(_ index : Int) {
let offset = CGPoint(x: view.frame.width * CGFloat(index), y: 0)
scrollView.setContentOffset(offset, animated: true)
}
Hope this what you are looking.
First line in function let offset = CGPoint(x: view.frame.width * CGFloat(index), y: 0), for calculate position where you want to move in scrollview. E.g : for first xib position will be CGPoint(x:0,y:0) and for 3rd xib, position will be CGPoint(x:200,y:0), consider each xib width is 100
setContentOffset use to scroll at specific location/point.
For more check Link

Why doesn't overriding `intrinsicContentSize` in `NSView` work for `NSScrollView`?

I have placed an instance of custom class BigView that is a subclass of NSView inside a NSScrollView in IB. The content size of my BigView will be computed at runtime. What is the best practices for setting the content size?
Overriding intrinsicContentSize, as suggested in the The Big Nerd Ranch guide, does not seem to work -- the frame remains at its original size:
class BigView: NSView {
...
override var intrinsicContentSize: NSSize { // doesn't work?!
return NSSize(width: 10000, height: 10000)
}
...
}
Setting the frame programmatically (or in IB) does work:
class BigView: NSView {
...
override func awakeFromNib() {
...
self.frame = NSRect(x: 0, y: 0, width: 10000, height: 10000)
}
...
}I
or from the controller:
class ViewController: NSViewController {
#IBOutlet weak var bigView: BigView!
...
override func viewDidLoad() {
super.viewDidLoad()
bigView.frame = NSRect(x: 0, y: 0, width: 1000, height: 1000)
}
...
}
This can also be done throughout the scrollview's documentView property:
class ViewController: NSViewController {
#IBOutlet weak var scrollView: NSScrollView!
...
override func viewDidLoad() {
super.viewDidLoad()
scrollView.documentView?.frame = NSRect(x: 0, y: 0,
width: 1000, height: 1000)
}
...
}
I suppose one could also use AutoLayout constraints as well.
What doesn't overriding intrinsicContentSize work?
The problem is that when translatesAutoresizingMaskIntoConstraints is enabled in newer versions of AppKit default constraints are set without considering the intrinsic size.
To fix this you need to add the constraints manually. First pin the position of your BigView to the top left of the superview (Should be the NSClipView). Then go to the Size Inspector, and in the Content Compression Resistance Priority section set Intrinsic Size to Placeholder. That gives your view the constraints it needs and everything should work at runtime.

viewController does not update

I have 2 UIViewControllers, one starting the GameScene and the other is supposed to show the highscore, as below. The problem is that my second UIViewController does not want to show the udpated highscore. It just keeps on showing 0 all the time.
Does anyone know what the problem is? I tried using viewDidLoad as well as viewWillAppear but none of them work.
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.text = String(GameScene().highScore)
self.view.addSubview(label)
}
in the GameScene, I have declared the highScore as so:
var highScore = Int()
and used in a gameOver function as so:
if score > highScore {highScore = score}
thanks
label.text = String(GameScene().highScore)
This piece of line suggests GameScene is a static class. Un less it is, you simply cannot access variables in such a manner.
You will need to pass data between view controllers to pass data between the scenes. You can pass data using one of three options:
Though performSegue()
performSegueWithIdentifier() and make a connection in IB
make a connection in IB and define an action

Add subviews without losing focus (firstResponder status) on NSView

How can I add subview without losing firstResponder status?
let scrollAreaRect = self.finalFrame
let scrolledView = NSView(frame: scrollAreaRect)
// Inserisco pulsanti di esempio
for i in 1 ... 10{
let bt = LevelButton(frame: NSRect( x: 0,
y: i*30,
width: Int(scrolledView.frame.width),
height: 30),
name: "Primo Livello",
number: 1)
scrolledView.addSubview(bt)
}
scrolledView.resignFirstResponder()
scrollView = NSScrollView(frame: scrollAreaRect)
scrollView.resignFirstResponder()
scrollView.documentView = scrolledView
scrollView.contentView.scrollToPoint((NSMakePoint(0, scrolledView.frame.size.height)))
self.addSubview(scrollView)
After run that code in initializer function, the NSView not calls more keyDown handle. How can I reassign firstResponder status to main view? Call resignFirstResponder on all subviews not works.