[swift-users] UIview frame and bounds properties

Jeff Kelley slaunchaman at gmail.com
Thu Apr 27 12:34:50 CDT 2017


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 at 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 at 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 at saagarjha.com <mailto:saagar at 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 at swift.org <mailto:swift-users at 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 at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170427/ed6a0189/attachment.html>


More information about the swift-users mailing list