Skip to content

Instantly share code, notes, and snippets.

View MaximAlien's full-sized avatar

Maxim Makhun MaximAlien

View GitHub Profile
@MaximAlien
MaximAlien / UIButtonExample
Created December 9, 2015 13:35
[iOS] Example code for adding UIButton programatically into the view of the UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 22, 200, 100)];
[button setTitle:@"Back" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
@MaximAlien
MaximAlien / SeparatorInsetZero
Created December 9, 2015 22:38
[iOS] Code which sets separator inset in the UITableViewCell to zero
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
{
[tableView setLayoutMargins:UIEdgeInsetsZero];
@MaximAlien
MaximAlien / OrientationChange
Created December 10, 2015 11:42
[iOS] Notification about orientation change
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
@MaximAlien
MaximAlien / BackNavBarItem
Created December 10, 2015 15:16
[iOS] Back button on navigation bar without text
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}
@MaximAlien
MaximAlien / ModalPresentation
Created December 16, 2015 11:19
[iOS] Modal UIViewController presentation (as UIModalPresentationFormSheet)
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navigationController animated:YES completion:nil];
@MaximAlien
MaximAlien / RotationAnimation
Created December 21, 2015 10:29
[iOS] Rotation animation using CABasicAnimation
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.duration = 2.0f;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.additive = YES;
rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI];
rotationAnimation.fillMode = kCAFillModeForwards;
[self.customView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
@MaximAlien
MaximAlien / ViewControllerPresentation
Created December 21, 2015 16:00
[iOS] Change corner radius and size of the UIViewController presented modally
// For UIViewController inside UINavigationController
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.navigationController.view.superview.bounds = CGRectMake(0, 0, 300, 300);
self.navigationController.view.superview.layer.cornerRadius = 2.0f;
}
// Usual presentation
@MaximAlien
MaximAlien / UIImageView
Created December 22, 2015 10:00
[iOS] UIImageView creation
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
imageView.image = [UIImage imageNamed:@"custom_image"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.bounds = CGRectInset(imageView.frame, 20.0f, 20.0f);
[self.navigationController.navigationBar addSubview:imageView];
@MaximAlien
MaximAlien / ViewAnimationStubs
Created December 24, 2015 07:43
[iOS] UIView animation stubs
[UIView animateWithDuration:1.0f animations:^{
}];
[UIView animateWithDuration:1.0f animations:^{
} completion:^(BOOL finished) {
}];
@MaximAlien
MaximAlien / GestureRecognizerExample
Created December 24, 2015 07:49
[iOS] UITapGestureRecognizer example on UIView
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handler:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];