Where to determine UIView size

后端 未结 3 1577
暗喜
暗喜 2021-02-01 04:03

Summary: How should the UIViewController know the size of its UIView instance when initialising that view?

The dedicated initialisation method for an UIView

相关标签:
3条回答
  • 2021-02-01 04:19

    You may want to calculate the bounds of the available screen space (which depends from the presence of other views like navigation bar, tab bar, and status bar). You can use these bounds then for the frame parameter in [[UIView alloc] initWithFrame:] in loadView.

    If this is what you want, then you might find my answer over here helpful: Determine the correct size in loadView

    0 讨论(0)
  • 2021-02-01 04:29

    You don't have to use the designated initializer. Just use init as in [[UIView alloc] init]. The designated initializer has to be used from subclasses' initializers.

    On the other hand, setting the frame twice should not do much harm. Performing a lot of tasks in setFrame: is unusual. Layouting is normally done in layoutSubviews and is only performed once.

    0 讨论(0)
  • 2021-02-01 04:32

    I'm currently trying to accomplish the same thing after subclassing from UIViewController to create a generic component and came to notice something quite weird :

    UIViewController has a "loadView" method that you may override to (quoting documentation) "This is where subclasses should create their custom view hierarchy".

    So, i create my view hierarchy, compositing my screen using a default frame size (since I may not be knowing the frame i should display things into at the time loadView is called).

    But then, upon what call do I correctly set frame sizes ? Aka : at what time is my view controller aware of it's view real size ?

    Do I have to create a "setFrame" in my ViewController subclass that the object creating the viewcontroller should call ? that seems weird.

    drvdijk : did you have a solution to your problem ?

    EDIT : just found this Note on UIViewController documentation : Note: You should not use view controllers to manage views that fill only a part of their window—that is, only part of the area defined by the application content rectangle. If you want to have an interface composed of several smaller views, embed them all in a single root view and manage that view with your view controller.

    So, it seems that UIViewController main view is always full screen. Period.

    That does pose a problem to me though, as I'm trying to create a custom UITabBarViewController (to be able to display custom images in the tabbar, and not blue gradient) : my tabBarViewController should displays subviewcontrollers into a part of the window only...

    0 讨论(0)
提交回复
热议问题