Skip to content

Instantly share code, notes, and snippets.

View MaximAlien's full-sized avatar

Maxim Makhun MaximAlien

View GitHub Profile
@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 / 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 / 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 / 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 / ImageFromColor
Created December 9, 2015 07:54
[iOS] Category which is used to create UIImage * from UIColor
@implementation UIImage (ImageFromColorCategory)
+ (UIImage *)imageFromColor:(UIColor *)color withSize:(CGSize)size
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
@MaximAlien
MaximAlien / HandleRotation
Created December 8, 2015 14:50
[iOS] Delegate method to handle rotation
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
}];
}
@MaximAlien
MaximAlien / AttrStrForButton
Last active May 24, 2019 15:08
[iOS] Attributed string for UIButton
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"test1"];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, [attributeString length])];
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0f] range:NSMakeRange(0, [attributeString length])];
NSMutableAttributedString *attributeNewString = [[NSMutableAttributedString alloc] initWithString:@"test2"];
[attributeString appendAttributedString:attributeNewString];
self.testButton.titleLabel.attributedText = attributeString;
// Image attachment
@MaximAlien
MaximAlien / DelegationUsingProtocol
Created December 3, 2015 21:56
[iOS] Delegation example
@protocol ExampleViewDelegate <NSObject>
- (void)doAction;
@end
@interface ExampleView : UIView
@property (assign) id<ExampleViewDelegate> delegate;
@MaximAlien
MaximAlien / iOSLayoutConstraintsFill
Created December 3, 2015 21:40
[iOS] Layout constraints to fill view in parent
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.tableView];
@MaximAlien
MaximAlien / iOSNavBarButtonItem
Created December 2, 2015 12:24
[iOS] Set right navigation bar item button
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(done)];
[self.navigationItem setRightBarButtonItem:doneButton];
}
- (void)done
{