(9/18) re-learn Standford_iOS7 development _ animation, automatic layout _ course notes, standford_ios7 Course
I recently started my internship. I don't have much time to update it.
Lesson 9:
1. demo of the previous lesson: complete implementation of Dropit
Https://github.com/NSLogMeng/Stanford_iOS7_Study/commit/cfc24c2c4300185d972cf151872d313414a48d32
2. Autolayout
Relatively simple please refer to courseware https://github.com/NSLogMeng/Stanford_iOS7_Study/blob/master/Slides/Lecture%209%20Slides.pdf
The course implementation method is visualized in storyboard. This article introduces two methods:
① NSLayoutConstraint
+ (Instancetype) attributes :( id) view1 attribute :( NSLayoutAttribute) attr1 relatedBy :( partial) relation toItem :( id) view2 attribute :( NSLayoutAttribute) attr2 multiplier :( CGFloat) multiplier constant :( CGFloat) c; // constraint: item1.attribute = (>=, <= related) multiplier * item2.attribute + constant // use UIView to add constraints-(void) addConstraint :( NSLayoutConstraint *) constraint
② Visual Format Language (VFL)
Official documents are easy to understand
Syntax:
Description |
Syntax |
Description |
Syntax |
Horizontal Direction |
H: |
Link |
>=, <=, = |
Vertical |
V: |
Space, Gap |
- |
Views |
[View] |
Priority |
@ Value |
SuperView |
| |
|
|
Example:
Code implementation example:
-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton * button1 = [[UIButton alloc] init]; button1.backgroundColor = [self randomColor]; [button1 setTitle: @ "button1" forState: UIControlStateNormal]; response = NO; // manually created code using VFL must change this attribute button1.layer. cornerRadius = 4.0f; [self. view addSubview: button1]; [self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-[button1]-|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings (button1)]; [self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "V: |-20-[button1 (width)]" options: 0 metrics: @ {@ "width": @ 30} views: callback (button1)]; UIButton * button2 = [[UIButton alloc] init]; button2.backgroundColor = [self randomColor]; [button2 setTitle: @ "button2" forState: UIControlStateNormal]; button2.translatesAutoresizingMaskIntoConstraints = NO; // manually created code using VFL must change this attribute button2.layer. cornerRadius = 4.0f; [self. view addSubview: button2]; [self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-[button2]-|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings (button2)]; [self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "V: [button1]-44-[button2 (30)]" options: 0 metrics: nil views: values (button1, button2)]; UITabBar * tabBar = [[UITabBar alloc] init]; tabBar. barTintColor = [UIColor colorWithRed: 102/255. 0f green: 204/255. 0f blue: 255/255. 0f alpha: 1.0f]; tabBar. translatesAutoresizingMaskIntoConstraints = NO; // manually created code using VFL must change this attribute UITabBarItem * item1 = [[UITabBarItem alloc] init]; [item1 setTitle: @ "item1"]; UITabBarItem * item2 = [[UITabBarItem alloc] init]; [item2 setTitle: @ "item2"]; UITabBarItem * item3 = [[UITabBarItem alloc] init]; [item3 setTitle: @ "item3"]; tabBar. items = @ [item1, item2, item3]; [self. view addSubview: tabBar]; [self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: | [tabBar] |" options: 0 metrics: nil views: NSDictionaryOfVariableBindings (tabBar)]; [self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "V: [tabBar (45)] |" options: 0 metrics: nil views: tables (tabBar)]; [button1 addTarget: self action: @ selector (touchButton1) forControlEvents: UIControlEventTouchDown];}
Note: ① The view created by the Code must be button1.translatesAutoresizingMaskIntoConstraints = NO to take effect.
② Metrics stores variable values (dictionaries) in VFL. For more information, see the constraints of button1.
③ Views parameter corresponds to the View value in VFL (implemented through NSDictionaryOfVariableBindings)
3. Homework
Not done yet = _ =, followed by complement.
Course video address: Netease Open Class: http://open.163.com/movie/2014/1/ B /P/M9H7S9F1H_M9H80K2BP.html
Or use iTunes U to search for standford courses