当前位置:网站首页>[oc]- < getting started with UI> -- common controls uibutton
[oc]- < getting started with UI> -- common controls uibutton
2022-07-06 08:58:00 【About Xiaosi】
List of articles
UIButton
UIButton Basics
- Buttons are all UI Very important components in the system , stay iOS Middle button UIButton The use of is also very flexible
Create text UIButton
- about UI object We should pay attention to some things when creating , establish UI Is the method of an object a class method or an instance method ?
- establish UI After object , Pay attention to the size , Location Color Wait for details
- Create a UIButton
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Can only be created through class methods buttonWithType
corner type :UIButtonTypeRoundedRect
Set up UIButton Properties of
- Set up button Button initialization position
btn.frame = CGRectMake(100, 100, 100, 40);
- Set button content
Parameters 1: String type , Display button text
Parameters 2: Set the text status type :UIControlStateNormal The normal state
[btn setTitle:@" Button 1" forState:UIControlStateNormal];
- Display text status type
IControlStateHighlighted Press status
[btn setTitle:@" Button press status " forState:UIControlStateHighlighted]
- The background color
btn.backgroundColor = [UIColor grayColor];
- Set the text color
Parameters 1 Color
Parameters 2 state UIControlStateNormal The normal state
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
- Press the status color
- Parameters 1 - Color
Parameters 2 - state UIControlStateHighlighted
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
- Set font size
btn.titleLabel.font = [UIFont systemFontOfSize:24];
Add to view screen display
[self.view addSubview:btn];
Without pictures UIButton effect
With pictures UIButton
- For normal applications We can't be buttons like this , Generally speaking, it is with pictures Button The buttons are pleasing to the eye Next, I'll show you how to create a picture Button
- The overall code
-(void) crateImageBtn {
// Create a custom type button
UIButton* btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame = CGRectMake(100, 200, 100, 100);
UIImage* icon01 = [UIImage imageNamed:@"btn01.jpeg"];
UIImage* icon02 = [UIImage imageNamed:@"btn02.jpeg"];
// How to set the button image
//p1 display Picture object
//p2 Control state
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
[self.view addSubview:btnImage];
}
With pictures Button Creation considerations and code interpretation
- Let's customize one first Button
- Our buttons should have two states Press and normal status
UIControlStateNormal Ordinary
UIControlStateHighlighted Press down
- The pictures corresponding to the two states are loaded - Picture name plus format
UIImage* icon01 = [UIImage imageNamed:@"btn01.jpeg"]; UIImage* icon02 = [UIImage imageNamed:@"btn02.jpeg"];
- How to set the button image
- Parameter one : Show picture objects
- Parameter two : Control state
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
effect
- Normal state
- Press status
UIButton Event handling
- The so-called event processing is to add the event we want to use when we press the button. When the user clicks the button, the event corresponding to the button will occur So as to achieve the function we want
- Add an event function to the button — One button can add multiple event functions
Multiple parameters of event processing
- Event handler
btn addTarget:(nullable id) action:(nonnull SEL) forControlEvents:(UIControlEvents)
First understand the meaning of parameters
1)
p1: Who will implement the event function , Who is the object of implementation a、
addTarget:self The implementer is himself
p2:@selector(pressBtn): Function object , When the button meets p3 When the event type , Call this function
p3:UIControlEvent: Event handler type
UIControlEventTouchUpOutside: When the finger leaves the screen and the position of the finger is outside the range of the button, the event function will be triggered
UIControlEventTouchDown: When our fingers just touch the screen
Implement event
-(void) createButton {
// Create fillets button
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 80, 40);
[btn setTitle:@" Button 1" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
//UIControlEventTouchUpInside: When the finger leaves the screen and the position of the finger is within the range of the button, the event function will be triggered
//2) Touch event function
[btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn];
// The same function can be called by different buttons
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(100, 100, 40, 80);
[btn2 setTitle:@" Button 2" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
// Set button tag -- I don't know the pointer of the button is written like this
btn.tag = 101;
btn2.tag = 102;
The function that the event needs to call ( One of the parameters )
// The parameter is the button itself that calls this function -- It is recommended to write like this
// Benefits are as follows
-(void) pressBtn:(UIButton*) btn {
if (btn.tag == 101) {
NSLog(@"btn1 pressed");
} else {
NSLog(@"btn2 pressed");
}
// NSLog(@"btn pressed");
effect
- Let's first click button one Then the button is touched – Print
- Click button 2 Then I was touched – Print
边栏推荐
- 注意力机制的一种卷积替代方式
- Show slave status \ read in G_ Master_ Log_ POS and relay_ Log_ The (size) relationship of POS
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
- LeetCode:39. 组合总和
- SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
- Leetcode: Jianzhi offer 04 Search in two-dimensional array
- Swagger setting field required is mandatory
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- [OC-Foundation框架]---【集合数组】
猜你喜欢
BMINF的後訓練量化實現
Delay initialization and sealing classes
[OC-Foundation框架]---【集合数组】
Tcp/ip protocol
[sword finger offer] serialized binary tree
Alibaba cloud server mining virus solution (practiced)
Navicat Premium 创建MySql 创建存储过程
【剑指offer】序列化二叉树
[OC]-<UI入门>--常用控件-UIButton
Using C language to complete a simple calculator (function pointer array and callback function)
随机推荐
LeetCode:124. Maximum path sum in binary tree
不同的数据驱动代码执行相同的测试场景
自定义卷积注意力算子的CUDA实现
LeetCode:387. The first unique character in the string
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
Problems encountered in connecting the database of the project and their solutions
LeetCode:394. 字符串解码
Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
LeetCode:498. Diagonal traversal
[OC-Foundation框架]--<Copy对象复制>
Notes 01
A convolution substitution of attention mechanism
项目连接数据库遇到的问题及解决
【嵌入式】使用JLINK RTT打印log
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
Deep anatomy of C language -- C language keywords
Advanced Computer Network Review(4)——Congestion Control of MPTCP
使用标签模板解决用户恶意输入的问题
vb.net 随窗口改变,缩放控件大小以及保持相对位置
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?