objective-c 如何访问当前固定格式页面的所有控件并进行修改 - ----------------[ 8] UILabel, height:12

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多帮助力 👁️ 130 次阅读

如何访问当前固定格式页面的所有控件并进行修改。当然若是随着内容不断变化的页面,你可能找不到对应的控件,所以无法修改。另外有的第三方库会来回改控件的属性,你就是把它的属性修改了,第三方库也可能重新修改回去。 我们经常用到第三方的SDK,只有头文件,拿不到源代码,那么就可以采用启动一个定时器扫描几次修改它的属性了(如控件的显示和隐藏,文本颜色)。 下面给出了打印当前页面的所有控件及编号。首先可以通过基本类型和位置来确定你修改的控件,若实在找不到通过编号也能找到。只要找到控件和编号你想怎么修改还不是轻而易举的事情。具体的修改控件的我就不再本文累述了。至于单窗口我也不写了,只写多窗口的情况

- (void)dumpView:(UIView *)aView atIndent:(int)indent into:(NSMutableString *)outstring

{

for (int i = 0; i < indent; i++) [outstring appendString:@"--"];

[outstring appendFormat:@"[%2d] %@, height:%f, origin.y:%f\n", indent, [[aView class] description], aView.frame.size.height, aView.frame.origin.y];

for (UIView *view in [aView subviews])

[self dumpView:view atIndent:indent + 1 into:outstring];

}

// Start the tree recursion at level 0 with the root view

- (NSString *) displayViews: (UIView *) aView

{ 拼多多帮助力入口

NSMutableString *outstring = [[NSMutableString alloc] init];

[self dumpView: aView atIndent:0 into:outstring];

return outstring;

}

// Show the tree

- (void)logViewTreeForMainWindow

{

NSLog(@"The view tree:\n%@", [self displayViews:BRGetKeyWindow()]);

}

/** 获取 keyWindow */

static inline UIWindow *BRGetKeyWindow(void) {

UIWindow *keyWindow = nil;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 // 编译时检查SDK版本:Xcode11+编译会调用(iOS SDK 13.0 以后版本的处理)

if (@available(iOS 13.0, *)) {

NSSet *connectedScenes = [UIApplication sharedApplication].connectedScenes;

for (UIScene *scene in connectedScenes) {

if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) {

UIWindowScene *windowScene = (UIWindowScene *)scene;

for (UIWindow *window in windowScene.windows) {

if (window.isKeyWindow) {

keyWindow = window;

break;

}

}

}

}

} else

#endif

{

#if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000

return [UIApplication sharedApplication].keyWindow;

#endif

}

return keyWindow;

}

使用的代码:

-(void)loginAction:(UIButton *)button{

[self logViewTreeForMainWindow];

return;

}

打印的日志:

The view tree:

[ 0] UIWindow, height:896.000000, origin.y:0.000000

--[ 1] UITransitionView, height:896.000000, origin.y:0.000000

----[ 2] UIDropShadowView, height:896.000000, origin.y:0.000000

------[ 3] UILayoutContainerView, height:896.000000, origin.y:0.000000

--------[ 4] UINavigationTransitionView, height:896.000000, origin.y:0.000000

----------[ 5] UIViewControllerWrapperView, height:896.000000, origin.y:0.000000

------------[ 6] UIView, height:896.000000, origin.y:0.000000

--------------[ 7] PPPasswordLoginView, height:896.000000, origin.y:0.000000

----------------[ 8] UIView, height:862.000000, origin.y:0.000000

------------------[ 9] UIButton, height:20.000000, origin.y:60.000000

--------------------[10] UIImageView, height:16.000000, origin.y:2.000000

--------------------[10] UIButtonLabel, height:0.000000, origin.y:0.000000

------------------[ 9] UIButton, height:20.000000, origin.y:331.000000

--------------------[10] UIButtonLabel, height:15.000000, origin.y:2.500000

------------------[ 9] UILabel, height:21.000000, origin.y:128.000000

------------------[ 9] UILabel, height:14.000000, origin.y:157.000000

------------------[ 9] UIView, height:1.000000, origin.y:257.000000

------------------[ 9] UIView, height:1.000000, origin.y:317.000000

------------------[ 9] UILabel, height:25.000000, origin.y:280.000000

------------------[ 9] UIButton, height:25.000000, origin.y:280.000000

--------------------[10] UIButtonLabel, height:0.000000, origin.y:0.000000

------------------[ 9] KeyBoardTextField, height:15.000000, origin.y:230.000000

--------------------[10] UITextFieldLabel, height:15.000000, origin.y:0.000000

--------------------[10] UIFieldEditor, height:15.000000, origin.y:0.000000

----------------------[11] _UITextLayoutCanvasView, height:15.000000, origin.y:0.000000

------------------------[12] _UITextLayoutFragmentView, height:15.000000, origin.y:-1.500000

------------------------[12] UITextSelectionView, height:0.000000, origin.y:0.000000

--------------------------[13] UIView, height:15.000000, origin.y:0.000000

------------------[ 9] KeyBoardTextField, height:15.000000, origin.y:290.000000

--------------------[10] UITextFieldLabel, height:15.000000, origin.y:0.000000

--------------------[10] _UITextLayoutCanvasView, height:15.000000, origin.y:0.000000

----------------------[11] _UITextLayoutFragmentView, height:15.000000, origin.y:-1.500000

------------------[ 9] UIButton, height:13.000000, origin.y:461.500000

--------------------[10] UIButtonLabel, height:15.000000, origin.y:-1.000000

------------------[ 9] UIButton, height:13.000000, origin.y:461.500000

--------------------[10] UIButtonLabel, height:15.000000, origin.y:-1.000000

------------------[ 9] UIButton, height:44.000000, origin.y:382.000000

--------------------[10] UIButtonLabel, height:21.000000, origin.y:11.500000

------------------[ 9] UIView, height:12.000000, origin.y:462.000000

------------------[ 9] DYSelectAgreeLoginPanel, height:13.000000, origin.y:817.000000

--------------------[10] UIButton, height:30.000000, origin.y:-9.000000

----------------------[11] UIButtonLabel, height:0.000000, origin.y:0.000000

--------------------[10] BGSwapButton, height:12.000000, origin.y:0.000000

----------------------[11] UIImageView, height:12.000000, origin.y:0.000000

--------------------[10] UILabel, height:13.000000, origin.y:0.000000

--------------------[10] UIButton, height:13.000000, origin.y:0.000000

----------------------[11] UIButtonLabel, height:15.000000, origin.y:-1.000000

--------------------[10] UILabel, height:13.000000, origin.y:0.000000

--------------------[10] UIButton, height:13.000000, origin.y:0.000000

----------------------[11] UIButtonLabel, height:15.000000, origin.y:-1.000000

--------------[ 7] DYLoginPanel, height:61.000000, origin.y:738.000000

----------------[ 8] UIView, height:0.500000, origin.y:6.250000

----------------[ 8] UILabel, height:13.000000, origin.y:0.000000

----------------[ 8] UIView, height:0.500000, origin.y:6.250000

----------------[ 8] UIButton, height:40.000000, origin.y:0.000000

------------------[ 9] UIImageView, height:40.000000, origin.y:0.000000

------------------[ 9] UIButtonLabel, height:0.000000, origin.y:0.000000

----------------[ 8] UIButton, height:40.000000, origin.y:0.000000

------------------[ 9] UIImageView, height:40.000000, origin.y:0.000000

------------------[ 9] UIButtonLabel, height:0.000000, origin.y:0.000000

----------------[ 8] UILabel, height:12.000000, origin.y:48.000000

----------------[ 8] UILabel, height:12.000000, origin.y:48.000000

注意:若你放在首页的viewWillAppear中打印可能打印出来的是The view tree: [ 0] (null), height:0.000000, origin.y:0.000000。因为该方法访问的是已经显示出来的控件(包括隐藏的控件),必定从你加载控件到实际显示出来之间需要时间,所以要用定时器扫描几次才能保证修改成功。

相关链接,objective

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧