This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
iPad Orientation Control help
Tried with this NY monkeypatching instead of swizzle because it is an instance method, not a class method. The def is called but no orientation change, and even no full screen.
Of course, it is not the main view controller. Sure, help will be needed.
from objc_util import * import ui def preferredInterfaceOrientationForPresentation(_self):#, _sel): self = ObjCInstance(_self) # UIViewController print('preferredInterfaceOrientationForPresentation:',self) return 3 # UIInterfaceOrientationLandscapeLeft @on_main_thread def main(): UIViewController = ObjCClass('UIViewController') vc = UIViewController.alloc().init() # monkeypatch of UIViewController class instance method UIViewController.preferredInterfaceOrientationForPresentation = preferredInterfaceOrientationForPresentation(vc) vc.setModalPresentationStyle_(2) # full screen l = ui.Label() l.frame = (10,10,100,32) l.text = 'label' vc.view().addSubview_(ObjCInstance(l)) rootvc = UIApplication.sharedApplication().keyWindow().rootViewController() rootvc.presentViewController_animated_completion_(vc, True, None) if __name__ == '__main__': main()
mcriley821 last edited by mcriley821
@cvp Got this to work by create_objc_class on the view controller to override the method, and by letting the system choose the modal presentation style (setting to 0)
changed your method to:
def preferredInterfaceOrientationForPresentation(_self,_cmd): return 3
And changed creation of vc and set modal to:
JonB last edited by
I think he meant to have an alloc().init() in there somewhere. You need an instance
Yea add an .alloc().init(). The anti-spam bot won’t let me add it on there. Sorry for the confusion
@mcriley821 yes, I already did it
@mcriley821 I tried this script. I'll let @stephen go on 😇
Thanks for your help. I'm happy we have now a new ObjectiveC guru because I only use it with a lot of tries.
Put in gist because refused as spam???
@jonb @mcriley821 I don't understand why my monkey patching was not good because the method was also called, proved by the print.
@cvp I’m no guru by any means lmao. It takes many crashes for me before I get something working. I’m steadily learning though.
My only guess on why monkey-patching didn’t work is because we already alloced and inited vc as a regular UIViewController and didn’t create another instance after overwriting the class method. But honestly I’ve never done monkey-patching so I’m not positive what I’m talking about
@mcriley821 But I was overriding an instance method, not a class method. And it worked because the def was called.
@mcriley821 I just tested with my old code and modal style 0, and it works in the same way as your code with the new objc class. The only difference is that the _cmd 2nd parameter is not accepted.
@cvp if you do
you still get 1, but if you
You get 3.
Your monkey-patch overwrites the UIViewController object. But if you do
The print has an error 'int object is not callable'.
Maybe it thinks the method is an attribute, and instead of overwriting the function it just makes an attribute
@mcriley821 you're right. I 🤐 and 😰
Wow.. ok let me go over this data and ill see what i come up with. im still learning ObjC/Swift. Thank ya'll so much for your input and time im off to crash Pythonista a few times lol i hope we can get this because it would be nice for everyone to have this ability without XCode..
@property(nonatomic, readonly) UIInterfaceOrientation preferredInterfaceOrientationForPresentation;
Just to make sure im understanding properly lol..
preferredInterfaceOrientationForPresentationwith 2 or more
UIInterfaceOrientationset on our
UIViewControllerto limit rotation but allowing rotation to differ from
UIStatusBar. and if we dont set this property it will keep the
UIViewControllerset to the current
Instead of trying to make the View's rotation differ from the status bar. why dont we just control the rotation of
UIStatusBaritself? or is that where iPadOS has the issue of ontrolling orientation of apps that supports split-screen multitasking.
JonB last edited by
@cvp you cannot monkey patch an objc object, without using the objc runtime (swizzleing). Monkey patching just affects calls in python -- when the system calls a selector it doesn't know anything about the python attribute.
There is a way to swizzle in objc, though as I recall, swizzling a single instance is tricky.
@stephen wow 🤔🤯🤕🤒
cvp last edited by cvp
@JonB Thanks for your explanation.
stephen last edited by stephen
@JonB would you happen to know of any good tutorials/walkthroughs/guides i can checkout on
objc_util..? im kinda grasping it but there just somthing missing for me to fully take her home...