当前位置:网站首页>[Objective-C] dynamically create controls

[Objective-C] dynamically create controls

2022-06-11 10:15:00 Back end coder

Create controls dynamically

//
// ViewController.m
// MediaIOS
//
// Created by Inke219223m on 2022/5/23.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


// Dynamic controls are created inside 
- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // Create button 
    //UIButton *ubOne = [[UIButton alloc] init];// Default 
    // Button type 
    //UIButton *ubOne = [UIButton buttonWithType:UIButtonTypeContactAdd];
    UIButton *ubOne = [UIButton buttonWithType:UIButtonTypeCustom];
    // Set button text   Default state 
    [ubOne setTitle:@" Click on me " forState:UIControlStateNormal];
    // The highlighted 
    [ubOne setTitle:@" Back end coder " forState:UIControlStateHighlighted];
    
    UIImage *uimg = [UIImage imageNamed:@" Hamburger "];
    UIImage *uimg2 = [UIImage imageNamed:@" French fries "];
    
    // Set default state picture 
    [ubOne setBackgroundImage:uimg forState:UIControlStateNormal];
    
    // Set highlight 
    [ubOne setBackgroundImage:uimg2 forState:UIControlStateHighlighted];
    
    // Set the default text yanse
    [ubOne setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [ubOne setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
    
    // Set button frame  To display 
    ubOne.frame = CGRectMake(50, 100, 100, 100);
    
    // Set click event 
    [ubOne addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
     
    // Add dynamically created buttons to 
    [self.view addSubview:ubOne];
    
}

-(void) buttonClick {
    
    NSLog(@" Click. , Click events ");
}
@end

原网站

版权声明
本文为[Back end coder]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110924148689.html