UIview frame and bounds properties


(Mohamed Salah) #1

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah


(Saagar Jha) #2

Would you mind sharing the code you’re having trouble with?

Saagar Jha

···

On Apr 27, 2017, at 10:22, Mohamed Salah via swift-users <swift-users@swift.org> wrote:

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #3

First of all, the swift-user list is mainly for Swift related question, which are not related to other frameworks like UIKit. You might find a better answer at Apple developer forums or on stackoverflow. :wink:

Second, this question is too general and not easy to answer without any code of yours. We need the scopes of your code to solve that issue.

···

--
Adrian Zubarev
Sent with Airmail

Am 27. April 2017 um 19:22:49, Mohamed Salah via swift-users (swift-users@swift.org) schrieb:

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Mohamed Salah) #4

Thanks for your support … here you are the piece of code

import UIKit

class FaceVeiw: UIView {
    
    /* it make error to use frame or bounds outside any functions WHY WHY WHY */

    let width = frame.size.width // (Gives an ERROR) frame property is not known here
    let width2 = bounds.size.width // Gives an ERROR) bound property is not know here as well
    
    override func draw(_ rect: CGRect)
    {
        let w = bounds.size.width // however bounds is known here
        let h = bounds.size.height
        
        let w2 = frame.size.width // frame as well known here
        let h2 = frame.size.height
        
    }
    
}

···

On Apr 27, 2017, at 9:23 PM, Saagar Jha <saagar@saagarjha.com> wrote:

Would you mind sharing the code you’re having trouble with?

Saagar Jha

On Apr 27, 2017, at 10:22, Mohamed Salah via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Jeff Kelley) #5

For this you’ll want to use computed properties. let isn’t the right choice since these aren’t constants—they change as the view resizes. Instead, computed properties using var will work:

extension UIView {

    var width: CGFloat { return frame.size.width }

}

That extension will add a width property to any UIView.

Jeff Kelley

SlaunchaMan@gmail.com | @SlaunchaMan <https://twitter.com/SlaunchaMan> | jeffkelley.org <http://jeffkelley.org/>

···

On Apr 27, 2017, at 1:28 PM, Mohamed Salah via swift-users <swift-users@swift.org> wrote:

Thanks for your support … here you are the piece of code

import UIKit

class FaceVeiw: UIView {
    
    /* it make error to use frame or bounds outside any functions WHY WHY WHY */

    let width = frame.size.width // (Gives an ERROR) frame property is not known here
    let width2 = bounds.size.width // Gives an ERROR) bound property is not know here as well
    
    override func draw(_ rect: CGRect)
    {
        let w = bounds.size.width // however bounds is known here
        let h = bounds.size.height
        
        let w2 = frame.size.width // frame as well known here
        let h2 = frame.size.height
        
    }
    
}

On Apr 27, 2017, at 9:23 PM, Saagar Jha <saagar@saagarjha.com <mailto:saagar@saagarjha.com>> wrote:

Would you mind sharing the code you’re having trouble with?

Saagar Jha

On Apr 27, 2017, at 10:22, Mohamed Salah via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #6

Have you tried using self.? It’s a good practice to always using self. to avoid issues where the compiler might use other globally available variables/constants functions.

···

--
Adrian Zubarev
Sent with Airmail

Am 27. April 2017 um 19:28:42, Mohamed Salah via swift-users (swift-users@swift.org) schrieb:

Thanks for your support … here you are the piece of code

import UIKit

class FaceVeiw: UIView {

/\* it make error to use frame or bounds outside any functions  WHY WHY WHY \*/

let width = frame\.size\.width // \(Gives an ERROR\) frame property is not known here
let width2 = bounds\.size\.width // Gives an ERROR\) bound property is not know here as well


override func draw\(\_ rect: CGRect\)
\{
    let w = bounds\.size\.width // however bounds is known here
    let h = bounds\.size\.height
    
    let w2 = frame\.size\.width // frame as well known here
    let h2 = frame\.size\.height
    
\}

}

On Apr 27, 2017, at 9:23 PM, Saagar Jha <saagar@saagarjha.com> wrote:

Would you mind sharing the code you’re having trouble with?

Saagar Jha

On Apr 27, 2017, at 10:22, Mohamed Salah via swift-users <swift-users@swift.org> wrote:

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Mohamed Salah) #7

yes I tried self. but it didn’t work as well ….

···

On Apr 27, 2017, at 9:30 PM, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Have you tried using self.? It’s a good practice to always using self. to avoid issues where the compiler might use other globally available variables/constants functions.

--
Adrian Zubarev
Sent with Airmail

Am 27. April 2017 um 19:28:42, Mohamed Salah via swift-users (swift-users@swift.org <mailto:swift-users@swift.org>) schrieb:

Thanks for your support … here you are the piece of code

import UIKit

class FaceVeiw: UIView {
    
    /* it make error to use frame or bounds outside any functions WHY WHY WHY */

    let width = frame.size.width // (Gives an ERROR) frame property is not known here
    let width2 = bounds.size.width // Gives an ERROR) bound property is not know here as well
    
    override func draw(_ rect: CGRect)
    {
        let w = bounds.size.width // however bounds is known here
        let h = bounds.size.height
        
        let w2 = frame.size.width // frame as well known here
        let h2 = frame.size.height
        
    }
    
}

On Apr 27, 2017, at 9:23 PM, Saagar Jha <saagar@saagarjha.com <mailto:saagar@saagarjha.com>> wrote:

Would you mind sharing the code you’re having trouble with?

Saagar Jha

On Apr 27, 2017, at 10:22, Mohamed Salah via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Saagar Jha) #8

The issue is that frame and bounds are only known after the initializer is run. Properties are set before the initializer, and they have can’t access frame and bounds.

Saagar Jha

···

On Apr 27, 2017, at 10:32, Mohamed Salah via swift-users <swift-users@swift.org> wrote:

yes I tried self. but it didn’t work as well ….

On Apr 27, 2017, at 9:30 PM, Adrian Zubarev <adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>> wrote:

Have you tried using self.? It’s a good practice to always using self. to avoid issues where the compiler might use other globally available variables/constants functions.

--
Adrian Zubarev
Sent with Airmail

Am 27. April 2017 um 19:28:42, Mohamed Salah via swift-users (swift-users@swift.org <mailto:swift-users@swift.org>) schrieb:

Thanks for your support … here you are the piece of code

import UIKit

class FaceVeiw: UIView {
    
    /* it make error to use frame or bounds outside any functions WHY WHY WHY */

    let width = frame.size.width // (Gives an ERROR) frame property is not known here
    let width2 = bounds.size.width // Gives an ERROR) bound property is not know here as well
    
    override func draw(_ rect: CGRect)
    {
        let w = bounds.size.width // however bounds is known here
        let h = bounds.size.height
        
        let w2 = frame.size.width // frame as well known here
        let h2 = frame.size.height
        
    }
    
}

On Apr 27, 2017, at 9:23 PM, Saagar Jha <saagar@saagarjha.com <mailto:saagar@saagarjha.com>> wrote:

Would you mind sharing the code you’re having trouble with?

Saagar Jha

On Apr 27, 2017, at 10:22, Mohamed Salah via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Mohamed Salah) #9

Thank you All … I am thrilled :slight_smile:
I appreciate your support and kind response …

have good time

regards

···

On Apr 27, 2017, at 9:37 PM, Saagar Jha <saagar@saagarjha.com> wrote:

The issue is that frame and bounds are only known after the initializer is run. Properties are set before the initializer, and they have can’t access frame and bounds.

Saagar Jha

On Apr 27, 2017, at 10:32, Mohamed Salah via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

yes I tried self. but it didn’t work as well ….

On Apr 27, 2017, at 9:30 PM, Adrian Zubarev <adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>> wrote:

Have you tried using self.? It’s a good practice to always using self. to avoid issues where the compiler might use other globally available variables/constants functions.

--
Adrian Zubarev
Sent with Airmail

Am 27. April 2017 um 19:28:42, Mohamed Salah via swift-users (swift-users@swift.org <mailto:swift-users@swift.org>) schrieb:

Thanks for your support … here you are the piece of code

import UIKit

class FaceVeiw: UIView {
    
    /* it make error to use frame or bounds outside any functions WHY WHY WHY */

    let width = frame.size.width // (Gives an ERROR) frame property is not known here
    let width2 = bounds.size.width // Gives an ERROR) bound property is not know here as well
    
    override func draw(_ rect: CGRect)
    {
        let w = bounds.size.width // however bounds is known here
        let h = bounds.size.height
        
        let w2 = frame.size.width // frame as well known here
        let h2 = frame.size.height
        
    }
    
}

On Apr 27, 2017, at 9:23 PM, Saagar Jha <saagar@saagarjha.com <mailto:saagar@saagarjha.com>> wrote:

Would you mind sharing the code you’re having trouble with?

Saagar Jha

On Apr 27, 2017, at 10:22, Mohamed Salah via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi ,

why UIview frame and bounds properties are not seen outside any functions ?

please advise

thank you
Mohamed Salah
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users